DateTime Formatting
Stay organized with collections
Save and categorize content based on your preferences.
DateTime values are expected to be in the ISO 8601 format, for example '2013-02-14T13:15:03-08:00' (YYYY-MM-DDTHH:mm:ssZ).
Below are examples for generating ISO 8601 datetime strings in a few popular programing languages.
Java
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateAsISOString = df.format(date);
JavaScript
var d = new Date();
var date = d.toISOString();
PHP
$objDateTime = new DateTime('NOW');
$isoDate = $objDateTime->format(DateTime::ISO8601);
Python
from datetime import date
d = date.now()
date = d.isoformat()
Ruby
require 'time'
d = Time.now
date = d.utc.iso8601
Perl
my $now = time();
$date = time2isoz($now);
C++
time_t now;
time(&now);
char buf[sizeof "2011-10-08T07:07:09Z"];
strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now));
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-03-24 UTC.
[null,null,["Last updated 2025-03-24 UTC."],[],[],null,["# DateTime Formatting\n\n[DateTime](/workspace/gmail/markup/reference/types/DateTime) values are expected to be in the ISO 8601 format, for example '2013-02-14T13:15:03-08:00' (YYYY-MM-DDTHH:mm:ssZ).\n\nBelow are examples for generating ISO 8601 datetime strings in a few popular programing languages. \n\n### Java\n\n Date date = new Date();\n DateFormat df = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ssZ\");\n String dateAsISOString = df.format(date);\n\n### JavaScript\n\n var d = new Date();\n var date = d.toISOString();\n\n### PHP\n\n $objDateTime = new DateTime('NOW');\n $isoDate = $objDateTime-\u003eformat(DateTime::ISO8601);\n\n### Python\n\n from datetime import date\n d = date.now()\n date = d.isoformat()\n\n### Ruby\n\n require 'time'\n d = Time.now\n date = d.utc.iso8601\n\n### Perl\n\n my $now = time();\n $date = time2isoz($now);\n\n### C++\n\n time_t now;\n time(&now);\n char buf[sizeof \"2011-10-08T07:07:09Z\"];\n strftime(buf, sizeof buf, \"%FT%TZ\", gmtime(&now));"]]