Date Utility Functions¶
We provide a set of functions for the date operations based on the datetime module of python. These functions have the same behavior as the functions provided by the formula column of SeaTable.
function import required
To use these functions, the dateutils module must be imported.
Timezone
If the input time string has a timezone info, it will be automatically converted to local time.
date¶
date
Return the ISO formatted date string.
Example
now¶
now
Return the ISO formatted date time of current and accurated to seconds.
Example
today¶
today
Return the ISO formatted current date time in string
Example
dateadd¶
dateadd
Addition operation for a datetime by different units such as years, months, weeks, days, hours, minutes and seconds, default by days.
Example
time_str = "2020-6-15"
time_str_s = "2020-6-15 15:23:21"
dateutils.dateadd(time_str, -2, 'years') # 2018-06-15
dateutils.dateadd(time_str, 3, 'months') # 2020-09-15
dateutils.dateadd(time_str_s, 44, 'minutes') # 2020-06-15 16:07:21
dateutils.dateadd(time_str_s, 1000, 'days') # 2023-03-12 15:23:21
dateutils.dateadd(time_str_s, 3, 'weeks') # 2020-07-06 15:23:21
dateutils.dateadd(time_str_s, -3, 'hours') # 2020-06-15 12:23:21
dateutils.dateadd(time_str_s, 3, 'seconds') # 2020-06-15 15:23:24
datediff¶
datediff
Caculation of the different between 2 date times by different units such as S, Y, D, H, M, YM, MD, YD.
- YM: The difference between the months in start_date and end_date. The days and years of the dates are ignored.
- MD: The difference between the days in start_date and end_date. The months and years of the dates are ignored.
- YD: The difference between the days of start_date and end_date. The years of the dates are ignored.
Example
time_start = "2019-6-1"
time_end = "2020-5-15"
dateutils.datediff(start=time_start, end=time_end, unit='S') # seconds 30153600
dateutils.datediff(start=time_start, end=time_end, unit='Y') # years 0
dateutils.datediff(start=time_start, end=time_end, unit='D') # days 349
dateutils.datediff(start=time_start, end=time_end, unit='H') # hours 8376
dateutils.datediff(start=time_start, end=time_end, unit='M') # months 11
dateutils.datediff(start=time_start, end=time_end, unit='YM') # 11
dateutils.datediff(start=time_start, end=time_end, unit='MD') # 14
dateutils.datediff("2019-1-28","2020-2-1", unit='YD') # 3
eomonth¶
eomonth
Return the last day of n months befor or after given date. Parameter months refers to n.
Example
year¶
month¶
months¶
months
Return the months difference of two given date.
Example
day¶
day
Return the day of given date.
Example
days¶
days
Return the days difference of two given date.
Example
hour¶
hour
Return the hour of given datetime.
Example
hours¶
hours
Return the hours difference of two given datetime.
Example
minute¶
minute
Return the minutes of given datetime.
Example
second¶
second
Return the seconds of given datetime.
Example
weekday¶
weekday
Return the weekday by recording 0 to 6 from Monday to Sunday.
Example
isoweekday¶
isoweekday
Return the weekday by recording 1 to 7 from Monday to Sunday based on ISO standard.
Example
weeknum¶
weeknum
Return the week number of given date by counting the 1st of Jan. as the first week.
Example
isoweeknum¶
isoweeknum
Return the week number of given date based on ISO standard.
Example
isomonth¶
isomonth
Return the ISO formatted month.
Example
quarter_from_yq¶
quarter_from_yq
Return a DateQuarter object, and params inlclude year and quarter..
Example
quarter_from_ym¶
quarter_from_ym
Return a DateQuarter object, and params include year and month.
Example
to_quarter¶
to_quarter
Return a DateQuarter object of a time string.
Example
quarters_within¶
quarters_within
Return a generator which will generate the DateQuater objects between a start date and end date. You can get the last quarter in the generator if you set param include_last=True
which is False
by default.
Example
Quarter operation¶
Quarter operation
Some operations are supported based on DateQuater object. Please refer the examples below:
q = dateutils.quarter_from_yq(2022, 3)
q.year # 2022
q.quarter # 3
q.start_date # 2022-07-01
q.end_date # 2022-09-30
q.days() # generator, which will generate the date in such quarter
list(q.days()) # [datetime.date(2022, 7, 1), datetime.date(2022, 7, 2),....., datetime.date(2022, 9, 30)]
q + 10 # <DateQuarter-2025,1Q>
q1 = dateutils.quater_from_yq(2021, 1) # <DateQuarter-2021,1Q>
q - q1 # 6
q < q1 # False
"2022-6-28" in q # False
"2022-8-28" in q # True
Other examples¶
Other examples
The date info returned can also be assigned as a param of dateutils. Here are some examples:
dt_now = dateutils.now() # 2022-02-07 09:49:14
# 1. date after 10 days
dt_10_days = dateutils.dateadd(dt_now, 10) # 2022-02-17 09:49:14
# 2. month after 10 days
dt_month_10_days = dateutils.month(dt_10_days) # 2
# 3. difference between 2 days
dt_10_days_before = dateutils.dateadd(dt_now, -10)
date_df = dateutils.datediff(dt_10_days_before, dt_10_days, unit="D") # 20
# 4. handle the time string with time-zone info with local timezone of "Asia/Shanghai" (UTC+8)
time_str = "2021-07-17T08:15:41.106+00:00"
time_day = dateutils.day(time_str) # 17
time_month = dateutils.month(time_str) # 7
time_year = dateutils.year(time_str) # 2021
time_hour = dateutils.hour(time_str) # 16
time_date = dateuitls.date(time_year, time_month, time_day) # 2021-07-17