Datetime Formats - ashish-greycube/help GitHub Wiki

Python

get today date

from frappe.utils import today, nowdate, now_datetime, nowtime

today_date = today() # 2024-11-11 <class 'str'>

today_date = nowdate() # 2024-11-11 <class 'str'>

today_datetime = now_datetime() # 2024-11-11 12:34:24.173512 <class 'datetime.datetime'>

current_time = nowtime() # 12:48:07.478841 <class 'str'>

Convert str date to datetime.date format

from frappe.utils import getdate, nowdate

today_date = nowdate() # 2024-11-11 <class 'str'>
today_date = getdate(nowdate()) # 2024-11-11 <class 'datetime.date'>

Add days, months, in date

from frappe.utils import getdate, nowdate, add_to_date, add_days, add_months

today_date = nowdate() # 2024-12-11 <class 'str'>

next_month = add_to_date(nowdate(), months=1) # 2025-01-11 <class 'str'>
previous_month = add_to_date(nowdate(), months=-1) # 2024-11-11 <class 'str'>
next_month = add_months(nowdate(), 1) # 2025-01-11 <class 'str'>
previous_month = add_months(nowdate(), -1) # 2024-11-11 <class 'str'>

next_day = add_to_date(nowdate(), days=1) # 2024-12-12 <class 'str'>
previous_day = add_to_date(nowdate(), days=-1) # 2024-12-10 <class 'str'>
next_day = add_days(nowdate(),1) # 2024-12-12 <class 'str'>
previous_day = add_days(nowdate(),-1) # 2024-12-10 <class 'str'>

Get first and last day of any month

from frappe.utils import get_first_day, get_last_day

current_month_start_date = get_first_day(date)
current_month_end_date = get_last_day(date)

Javascript

Get first and last day of current month

frappe.datetime.month_start()
frappe.datetime.month_end()

Get today date and add or subtract from date

let today = frappe.datetime.nowdate()   // '2024-12-02' <'string'>
let one_month_before_today = frappe.datetime.add_days(frappe.datetime.nowdate(), -30)  // '2024-12-02' <'string'>
⚠️ **GitHub.com Fallback** ⚠️