DateTime
Date and time creation, parsing, and manipulation
Dates and times. Static methods make one, instance methods compare, format and do arithmetic.
Properties
yearNumberYear component
monthNumberMonth component (1-12)
dayNumberDay of month
hourNumberHour component (0-23)
minuteNumberMinute component (0-59)
secondNumberSecond component (0-59)
weekdayStringDay name ("sunday" to "saturday")
Methods
.format(pattern)StringFormat this date using a pattern (e.g. 'yyyy-MM-dd HH:mm').
- pattern String required
.relative()StringGet a relative time string (e.g. '2 hours ago').
DateTime.now()DateTime ›Get the current date and time.
DateTime.today()DateTime ›Get today's date at midnight.
DateTime.from(options)DateTime ›Create a date from components: { year, month, day, hour, minute, second }.
- options Any required
Anything left out defaults to the current value for that component.
- year Number
Defaults to the current year.
- month Number
1 to 12.
- day Number
1 to 31.
- hour Number
0 to 23.
- minute Number
0 to 59.
- second Number
0 to 59.
- year Number
Examples
Current date & formatting
const now = DateTime.now();
const formatted = now.format("MMM d, yyyy");
// "Mar 13, 2026"Parsing & comparison
const date = DateTime.parse("2026-01-01");
const today = DateTime.today();
console.log(date.isBefore(today)); // trueDate arithmetic
const tomorrow = DateTime.now()
.add(Duration.days(1));
const diff = tomorrow.difference(DateTime.now());
console.log(diff.inHours); // ~24