Dates & Times

The power of now!

Working with Dates in BoxLang

BoxLang provides comprehensive date and time functionality through its DateTime object and extensive collection of temporal Built-in Functions (BIFs). All date and time operations in BoxLang work with the unified DateTime type, which internally wraps Java's ZonedDateTime to provide robust timezone and formatting support.

The DateTime Object

The DateTime object is the cornerstone of all temporal operations in BoxLang. It represents a specific moment in time with timezone information and provides methods for formatting, manipulation, and comparison. DateTime objects are created using BoxLang's temporal BIFs and provide member methods for advanced operations.

Creating DateTime Objects

BoxLang provides several Built-in Functions (BIFs) for creating DateTime objects:

Current Date and Time

// Get current date and time in system timezone
now = now()

From String Representations

// Parse from various string formats
dt = parseDateTime("December 25, 2023")
dt = parseDateTime("2023-12-25T15:30:00Z")
dt = parseDateTime("25-Dec-2023 3:30 PM")

// Parse with specific locale and timezone
dt = parseDateTime("25/12/2023", "en_US", "America/New_York")

From Numeric Components

Creating Time Spans

📚 Temporal Built-In Functions (BIFs)

BoxLang provides a comprehensive set of temporal BIFs organized by functionality. All temporal BIFs can also be called as member methods on DateTime objects.

🔨 Creation Functions

Function
Purpose
Example

now()

Current date/time

now() → Current DateTime

createDate()

Create date from components

createDate(2023, 12, 25)

createDateTime()

Create date with time

createDateTime(2023, 12, 25, 15, 30, 45)

createTime()

Create time only

createTime(15, 30, 45)

createTimeSpan()

Create duration

createTimeSpan(1, 2, 30, 45)

parseDateTime()

Parse from string

parseDateTime("2023-12-25")

🎨 Formatting Functions

Function
Purpose
Example

dateFormat()

Format date only

dateFormat(now(), "mm/dd/yyyy")

timeFormat()

Format time only

timeFormat(now(), "HH:nn:ss")

dateTimeFormat()

Format date and time

dateTimeFormat(now(), "yyyy-mm-dd HH:nn:ss")

lsDateFormat()

Locale-specific date format

lsDateFormat(now(), "medium", "en_US")

lsTimeFormat()

Locale-specific time format

lsTimeFormat(now(), "short", "en_US")

➕ Manipulation Functions

Function
Purpose
Example

dateAdd()

Add/subtract date units

dateAdd("d", 5, now())

dateConvert()

Convert between timezones

dateConvert("local2utc", now())

📊 Calculation Functions

Function
Purpose
Example

dateDiff()

Difference between dates

dateDiff("d", date1, date2)

dateCompare()

Compare two dates

dateCompare(date1, date2)

🔍 Extraction Functions

Function
Purpose
Example

year()

Extract year

year(now())2023

month()

Extract month (1-12)

month(now())12

day()

Extract day of month

day(now())25

hour()

Extract hour (0-23)

hour(now())15

minute()

Extract minutes

minute(now())30

second()

Extract seconds

second(now())45

dayOfWeek()

Day of week (1-7)

dayOfWeek(now())3

dayOfYear()

Day of year (1-366)

dayOfYear(now())359

quarter()

Quarter of year (1-4)

quarter(now())4

week()

Week of year

week(now())52

📝 String Representation Functions

Function
Purpose
Example

monthAsString()

Full month name

monthAsString(12)"December"

monthShortAsString()

Abbreviated month

monthShortAsString(12)"Dec"

dayOfWeekAsString()

Full day name

dayOfWeekAsString(3)"Tuesday"

dayOfWeekShortAsString()

Abbreviated day

dayOfWeekShortAsString(3)"Tue"

📏 Information Functions

Function
Purpose
Example

daysInMonth()

Days in a month

daysInMonth(now())31

daysInYear()

Days in a year

daysInYear(now())365

firstDayOfMonth()

First day of month

firstDayOfMonth(now())

isLeapYear()

Check if leap year

isLeapYear(2024)true

🌍 Timezone Functions

Function
Purpose
Example

setTimezone()

Set request timezone

setTimezone("America/New_York")

getTimezone()

Get current timezone

getTimezone()"EST"

getTimezoneInfo()

Timezone information

getTimezoneInfo()

clearTimezone()

Clear request timezone

clearTimezone()

offset()

Get timezone offset

offset(now())-5

🔧 Utility Functions

Function
Purpose
Example

getNumericDate()

Days since epoch

getNumericDate(now())

getTime()

Milliseconds since epoch

getTime(now())

⚙️ Member Functions

All temporal BIFs can be called as member functions on DateTime objects:

Try it online!

Date Time Format Masks

BoxLang supports extensive formatting options through format masks:

Basic Date and Time Components

Mask
Description
Example

d

Day of month, no leading zero

5, 25

dd

Day of month, leading zero

05, 25

EEE

Day of week, three-letter abbreviation

Mon, Tue

EEEE

Day of week, full name

Monday, Tuesday

m

Month, no leading zero

1, 12

mm

Month, leading zero

01, 12

mmm

Month, three-letter abbreviation

Jan, Dec

mmmm

Month, full name

January, December

M

Month in year

1, 12

D

Day in year

1, 365

yy

Year, last two digits

23

yyyy

Year, four digits

2023

YYYY

Week year, four digits

2023

Y

Week year

2023

YY

Week year, last two digits

23

G

Period/era string

BC, AD

Time Components

Mask
Description
Example

h

Hours (12-hour), no leading zero

3, 11

hh

Hours (12-hour), leading zero

03, 11

H

Hours (24-hour), no leading zero

15, 23

HH

Hours (24-hour), leading zero

15, 23

n

Minutes, no leading zero

5, 30

nn

Minutes, leading zero

05, 30

s

Seconds, no leading zero

5, 45

ss

Seconds, leading zero

05, 45

l or L

Milliseconds, no leading zeros

123

t

One-character time marker

A, P

tt

Time marker string

AM, PM

Week Components

Mask
Description
Example

w

Week of year, single digit

1, 52

ww

Week of year, leading zero

01, 52

W

Week of month, single digit

1, 5

WW

Week of month, leading zero

01, 05

Predefined Full Date/Time Formats

These masks format the complete date and time and cannot be combined with other masks:

Mask
Equivalent Format
Example

short

"m/d/y h:nn tt"

12/25/23 3:30 PM

medium

"mmm d, yyyy h:nn:ss tt"

Dec 25, 2023 3:30:45 PM

long

Medium with full month and timezone

December 25, 2023 3:30:45 PM EST

full

"EEEE, mmmm d, yyyy h:mm:ss tt EST"

Monday, December 25, 2023 3:30:45 PM EST

iso

ISO8601 format

2023-12-25T15:30:45-05:00

Formatting Dates

Using Built-in Functions

Timezone Support

BoxLang supports timezone specification using:

  • 3-letter codes: "EST", "UTC", "PST"

  • Full timezone names: "America/New_York", "Europe/London", "Asia/Tokyo"

Working with Timezones

BoxLang provides timezone support at two levels: request-level and formatting-level.

Request-Level Timezone Management

These BIFs set the timezone for the current request only. If not set, BoxLang uses the runtime configuration timezone.

Formatting with Timezones

When formatting dates, you can specify a timezone without affecting the request timezone:

Supported Timezone Formats

  • 3-letter codes: "EST", "UTC", "PST", "CST"

  • Full timezone names: "America/New_York", "Europe/London", "Asia/Tokyo"

DateTime Member Methods

DateTime objects provide numerous member methods for manipulation, formatting, and information retrieval. These methods can be called directly on DateTime instances using dot notation.

Comparison and Equality Methods

Cloning and Copying

Conversion and Output Methods

Date/Time Component Access

Date Manipulation Methods

Timezone Operations

Formatting Methods

Advanced Member Method Usage

Working with Temporal Units

Range and Validation

These member methods provide direct access to the rich functionality of BoxLang's DateTime objects, allowing for fluent and expressive date manipulation code. Most operations return new DateTime instances, supporting method chaining for complex transformations.

Date Arithmetic and Mathematical Operations

BoxLang supports mathematical operations on dates, converting them to fractional days for calculations. For basic arithmetic operations (addition, subtraction, multiplication, and division), dates and timespans can be cast as numeric values. In BoxLang, the numeric value of a DateTime object represents the decimal days since the Unix epoch time.

A timespan or Java Duration object is represented as decimal days for the purpose of mathematical operations.

Addition and Subtraction

Multiplication and Division

Using DateAdd for Precision

Date Comparison and Differences

Extracting Date Parts

BoxLang provides numerous BIFs to extract specific parts of dates:

Utility Functions

Validation and Information

Creating Time Spans

Common Patterns and Examples

Working with Business Days

Important Note: The above assumes a locale where the starting day of the week is Sunday. The dayOfWeek value returned for a given date will depend on the server locale ( e.g. The first day of the week for most of Europe is Monday, whereas the first day of the week in Egypt is Saturday ) unless you specify a locale argument to ensure consistency: dayOfWeek( date=result, locale="en_US" )

Date Range Generation

Age Calculation

Timezone Conversion Utility

Performance Considerations

  • DateTime objects are immutable; operations return new instances

  • Use BIFs like dateAdd(), dateDiff(), dateFormat() for most operations

  • Cache formatted strings if formatting the same date multiple times with dateTimeFormat()

  • Be mindful of timezone conversions in loops

  • Use createTimeSpan() for duration calculations rather than manual arithmetic

Last updated

Was this helpful?