Skip to main content Link Search Menu Expand Document (external link)

date() : Convert to DateTime


On this page

Summary

Convert a string or number to a DateTime object.

Signature

date(value)

Parameters

Parameter Type Required Description
value String/Number Yes The value to convert to DateTime

Returns

Type: DateTime

A DateTime object representing the parsed date.


Supported Formats

Format Example Description
ISO 8601 "2024-03-15" yyyy-MM-dd
ISO DateTime "2024-03-15T14:30:00" With time component
US Format "3/15/2024" M/d/yyyy
Long Format "March 15, 2024" Full month name
Unix Timestamp 1710511200 Seconds since 1970-01-01

Examples

Parse ISO Date

<p>Order Date: </p>

Data:

doc.Params["model"] = new {
    orderDate = "2024-03-15"
};

Output:

<p>Order Date: March 15, 2024</p>

Calculate Days Until

<p>Days until deadline: </p>

Data:

doc.Params["model"] = new {
    today = new DateTime(2024, 3, 1),
    deadline = "2024-03-31"
};

Output:

<p>Days until deadline: 30</p>

Format Different Date Strings


  <div class="event">
    <h3></h3>
    <p></p>
  </div>

Data:

doc.Params["model"] = new {
    events = new[] {
        new { name = "Conference", dateString = "2024-04-15" },
        new { name = "Workshop", dateString = "2024-05-20" },
        new { name = "Seminar", dateString = "2024-06-10" }
    }
};

Output:

<div class="event">
  <h3>Conference</h3>
  <p>Monday, April 15, 2024</p>
</div>
<div class="event">
  <h3>Workshop</h3>
  <p>Monday, May 20, 2024</p>
</div>
<div class="event">
  <h3>Seminar</h3>
  <p>Monday, June 10, 2024</p>
</div>

Add Days to String Date

<p>Ship Date: </p>
<p>Est. Delivery: </p>

Data:

doc.Params["model"] = new {
    orderDate = "2024-03-15"
};

Output:

<p>Ship Date: 2024-03-15</p>
<p>Est. Delivery: 2024-03-20</p>

Notes

  • Automatically detects common date formats
  • Throws exception if string cannot be parsed as date
  • Use with date manipulation functions (addDays, addMonths, etc.)
  • Always store dates as DateTime in model when possible
  • For formatting output, use format() function
  • Time zone handling depends on input format

See Also