Python Pendulum Difference














































Python Pendulum Difference



                          Difference

 The diff() method returns a Period instance that represents the total duration between two DateTime instances. This interval can be then expressed in various units. These interval methods always return the total difference expressed in the specified time requested. All values are truncated and not rounded.


The diff() method has a default first parameter which is the DateTime instance to compare to, or None if you want to use now(). The 2nd parameter is optional and indicates if you want the return value to be the absolute value or a relative value that might have a - (negative) sign if the passed in date is less than the current instance. This will default to True, return the absolute value.


     #CODE

import pendulum

dt_ottawa = pendulum.datetime(2022, 7, 7, tz='America/Toronto')
print(dt_ottawa)
#datetime in Toronto

dt_vancouver = pendulum.datetime(2022, 7, 7, tz='America/Vancouver')
print(dt_vancouver)
#datetime in Vancouver

print(dt_ottawa.diff(dt_vancouver).in_hours())

print(dt_ottawa.diff(dt_vancouver, False).in_hours())

print(dt_vancouver.diff(dt_ottawa, False).in_hours())

dt = pendulum.datetime(2022, 1, 31, 0)

print(dt.diff(dt.add(months=1)).in_days())

print(dt.diff(dt.subtract(months=1), False).in_days())

dt = pendulum.datetime(2012, 4, 30, 0)

print(dt.diff(dt.add(months=1)).in_days())

print(dt.diff(dt.add(weeks=1)).in_days())

dt = pendulum.datetime(2012, 1, 1, 0)
print(dt.diff(dt.add(seconds=59)).in_minutes())

print(dt.diff(dt.add(seconds=60)).in_minutes())

print(dt.diff(dt.add(seconds=119)).in_minutes())

print(dt.diff(dt.add(seconds=120)).in_minutes())

     #OUTPUT

2022-07-07T00:00:00-04:00
2022-07-07T00:00:00-07:00
3
3
-3
28
-31
30
7
0
1
1
2



More Articles of Anmol Agrawal:

Name Views Likes
Python Pendulum Why to use Pendulum 81 0
Python Pendulum Limitations in DJANGO 116 0
Python Pendulum Limitations in MYSQLCLIENT 91 0
Python Pendulum Limitations in SQLITE3 95 0
Python Pendulum Testing 2 101 0
Python Pendulum Testing 114 0
Python Pendulum Range 110 0
Python Pendulum Period 3 108 0
Python Pendulum Period 2 103 0
Python Pendulum Period 100 0
Python Pendulum Properties and Duration Methods 2 98 0
Python Pendulum Properties and Duration Methods 106 0
Python Pendulum Instantiation 107 0
Python Pendulum Duration 104 0
Python Pendulum TImezone usage 2 94 0
Python Pendulum Timezone usage 93 0
Python Pendulum Switching Timezones 100 0
Python Pendulum Shifting Time to Transition 112 0
Python Pendulum Normalization 109 0
Python Pendulum Modifiers 2 110 0
Python Pendulum Modifiers 106 0
Python Pendulum Difference for Humans 2 141 0
Python Pendulum Difference for Humans 114 0
Python Pendulum Difference 116 0
Python pendulum Substraction 128 0
Pyhton pendulum Addition 123 0
Python pendulum Comapaison part 2 116 0
Python Pendulum Comparison 119 0
Python Pendulum Localized Formats and Escaping characteristics 117 0
Python pendulum Tokens 124 0
Python pendulum Formatter 117 0
Python pendulum Common Formats 122 0
Python pendulum String Formatting 123 0
Python pendulum Fluent helpers 2 116 0
Python pendulum Fluent helpers 106 0
Python pendulum Attributes and Properties 111 0
Python pendulum Localization 135 118
Python pendulum parsing 2 131 23
Python pendulum Parsing 1 138 61
Python pendulum 2 145 57
Python pendulum basics 136 9
python library Pendulum 137 33
python winsound.SND_PURGE and winsound.SND_WAIT 149 5
Python winsound.SND_NOSTOP 179 1
Python winsound.SND_NODEFAULT 158 1
python winsound.SND_ASYNC not excecuting problem 164 2
Python winsound functions 181 3
python winsound.SND_ALIAS 190 1
Python winsound.MessageBeep() 205 1
Python winsound basic introduction 241 1

Comments