DateString

DateString[]

gives a string representing the complete current local date and time.

DateString["elem"]

gives the specified element or format for date and time.

DateString[{"elem1","elem2",}]

concatenates the specified elements in the order given.

DateString[{y,m,d,h,m,s}]

gives a string corresponding to a DateList specification.

DateString[time]

gives a string corresponding to an AbsoluteTime specification.

DateString[date]

gives a string corresponding to a DateObject specification.

DateString[spec,elems]

gives elements elems of the date or time specification spec.

Details and Options

  • DateString[] by default gives its output in the form specified by $DateStringFormat.
  • The following date format specifications can be given as elements:
  • "Date"full dateThursday 4 April 2019
    "DateShort"short dateThu 4 Apr 2019
    "Time"full time15:57:49
    "DateTime"full date and timeThursday 4 April 2019 15:57:57
    "DateTimeShort"short date and timeThu 4 Apr 2019 15:58:08
    "ISODate"ISO-8601 date2019-04-04
    "ISOWeekDate"ISO week date2019-W14-4
    "ISOOrdinalDate"ISO ordinal date2019-094
    "ISODateTime"ISO date and time2019-04-04T15:58:57
  • Elements related to year include the following:
  • "Year"full year2005
    "YearShort"2-digit year05
    "YearExact"year with fraction2005.26
    "YearFraction"fractional year0.256625
    "ISOYear"ISO-8601 year2005
    "YearUnsigned"year without sign2005
    "YearRomanNumeral"year in Roman numeralsMMV
    "ADBC"AD or BCAD
    "CEBCE"CE or BCEBCE
  • Elements related to month include the following:
  • "Month"2digit month number08
    "MonthShort"1 or 2digit month number8
    "MonthExact"month including fraction8.26759
    "MonthFraction"fractional part of month0.267586
    "MonthName"month nameAugust
    "MonthNameShort"month name in short formAug
    "MonthNameInitial"first letter of month nameA
    "MonthRomanNumeral"month in Roman numeralsVIII
    "MonthRomanNumeralLowerCase"lowercase Roman numeralsviii
  • Elements related to day of the month include the following:
  • "Day"2digit day of the month09
    "DayShort"1 or 2digit day9
    "DayExact"day including fraction9.29518
    "DayFraction"fractional part of day0.295176
  • Elements related to day of the week include the following:
  • "DayName"day of the weekWednesday
    "DayNameShort"short day of the weekWed
    "DayNameInitial"first letter of day nameW
    "ISOWeekDay"ISO-8601 day number4
    "ISOWeekDayExact"day number with fraction4.29518
    "ISOWeekDayFraction"fractional day number0.295176
  • Elements related to hour include the following:
  • "Hour"
  • system 2digit hour
  • 19
    "Hour12"2-digit 12-hour clock07
    "Hour24"2-digit 24-hour clock19
    "HourShort"system 1- or 2 digit hour19
    "Hour12Short"1- or 2-digit 12-hour clock7
    "Hour24Short"1- or 2-digit 24-hour clock19
    "HourExact"hour including fraction7.08422
    "HourFraction"fractional part of hour0.0842222
    "AMPM"AM or PMPM
    "AMPMLowerCase"am or pmpm
  • Elements related to minute include the following:
  • "Minute"2-digit minute05
    "MinuteShort"1- or 2-digit minute5
    "MinuteExact"minute including fraction5.05333
    "MinuteFraction"fractional part of minute0.0533333
  • Elements related to second include the following:
  • "Second"2digit seconds03
    "SecondShort"1 or 2digit seconds3
    "SecondExact"seconds including fractions03.199
    "SecondFraction"fractional part of second0.2
    "Millisecond"3digit milliseconds019
    "MillisecondShort"1, 2, or 3digit milliseconds19
    "MillisecondExact"millisecond with fraction019.99
    "MillisecondFraction"fractional part of millisecond0.999809
  • Elements larger than a year include the following:
  • "Millennium"beginning with year X000-3000
    "MillenniumUnsigned"unsigned millennium3000
    "MillenniumBeginning01"beginning with year X0012001
    "Century"beginning with year XX00-2100
    "CenturyUnsigned"unsigned century2100
    "CenturyBeginning01"beginning with year XX012001
    "Decade"10 year period-2010
    "DecadeUnsigned"unsigned decade2010
  • Elements related to multiple date elements include the following:
  • "Quarter"quarter number1
    "QuarterName"quarter of yearQuarter 1
    "QuarterNameShort"quarter of year in short formQ1
    "Week"2digit week number 02
    "WeekShort"1- or 2digit week number 2
    "WeekExact"week including fraction2.44048
    "WeekFraction"fractional week number0.440478
  • Additional ISO-8601 date elements include the following:
  • "ISOYearDay"3digit day of the year094
    "ISOYearDayShort"1, 2 or 3digit day of year94
    "ISOYearDayExact"year day including fraction94.295
    "ISOYearDayFraction"fractional part of year day0.295176
  • Elements related to time zone include the following:
  • "TimeZoneGMTRelative"GMT offsetGMT-04:00
    "ISOTimeZone"ISO-8601 GMT offset-04:00
    "TimeZoneName"time zone nameEastern Standard Time
    "TimeZoneNameShort"short time zone nameEST
  • Any other string given in the list of elements is concatenated literally in the output string.
  • With a typical value for $DateStringFormat, DateString[{y,m,d,h,m,s}] will truncate fractional seconds.
  • Shorter lists can be used in DateString[{y,m,}]: {y} is equivalent to {y,1,1,0,0,0}, {y,m} to {y,m,1,0,0,0}, etc.
  • Values of m, d, h, m, s outside their normal ranges are appropriately reduced. Noninteger values of d, h, m, s can also be used. »
  • In DateString[spec,elems], the following date and time specifications can be given as spec:
  • timeabsolute time specification
    {y,m,d,h,m,s}DateList specification
    "string"DateString output
    {"string",{"e1","e2",}}date string formed from specific elements
  • In the form DateString[{"string",{"e1","e2",},], the "ei" can be "Year", "YearShort", "Quarter", "Month", "MonthName", "Day", "DayName", "Hour", "Hour12", "AMPM", "Minute", or "Second".
  • DateString[{"string",{"e1","e2",},] uses the "ei" to fill in elements of {y,m,d,h,m,s}. Those not filled in are taken to have default values {yc,1,1,0,0,0}, where yc is the current year. »
  • In DateString[{"string",{"e1","e2",},], the "ei" are extracted from "string" in the order given, and can be separated by any non-alphanumeric characters. »
  • DateString[{"string",{"e1","sep12","e2","sep23",},] extracts elements using the explicit separators specified. »
  • For "*Exact" elements (such as "SecondExact" and "MonthExact"), the integer part of the returned value will match the value given by the normal element (i.e. "Second" and "Month").
  • DateString[date] uses the DateFormat value of the specified DateObject to determine the elements included in the resulting string.
  • In DateString[spec,elems], the time and date corresponding to spec are by default assumed to be in the time zone specified by $TimeZone, unless spec explicitly specifies another time zone.
  • DateString[spec,elems,TimeZone->z] gives output converted to time zone z.
  • DateString[] uses whatever date and time have been set on your computer system. It performs no corrections for time zones, daylight saving time, etc.
  • DateString[TimeZone -> z] gives the date and time inferred for time zone z by assuming that your computer is set for the time zone specified by $TimeZone. »

Examples

open allclose all

Basic Examples  (4)

Find the current date:

The current date in a specific format:

Another date in a specific format:

DateString supports a number of ISO-8601 date formats:

Scope  (9)

Find the day of the week corresponding to a date:

Date lists are converted to standard normalized form:

Strings are automatically interpreted when possible:

Both DateObject and TimeObject expressions include default DateFormat values:

Separators for input strings are assumed if not specified:

Give explicit separators for an input date string:

Day, hour, minute, and second values can be nonintegers:

The current year is used if not specified in a string:

Convert an absolute time to a date string:

Options  (1)

TimeZone  (1)

Find the current date in Greenwich, United Kingdom:

Get a date string for a specific date using a time zone string:

Applications  (1)

Make a dynamically updating clock:

Properties & Relations  (6)

DateObject represents a given date:

AbsoluteTime represents dates as seconds since the beginning of 1900:

DateList represents dates as lists of date elements:

DateString represents dates as strings:

Use $DateStringFormat to define the output formatting:

DateString gives results that match a DatePattern:

Use DateListPlot to plot data with date string coordinates:

Use DatePlus to add time units to a date string:

Use DateDifference to find the number of time units between dates:

Use Order and DateList to determine the order of dates:

Possible Issues  (3)

Explicit elements may be needed to interpret ambiguous strings:

For numeric TimeZone values, the elements "TimeZoneName" and "TimeZoneNameShort" are returned as GMT offsets:

When using a named TimeZone, an appropriate "TimeZoneName" will be returned:

The element "WeekExact" uses the same integer part as "Week" and thus will never return a value smaller than 1:

The same is true for all zero-indexed elements:

Introduced in 2007
 (6.0)
 |
Updated in 2014
 (10.0)
2015
 (10.2)
2017
 (11.1)
2018
 (11.3)
2019
 (12.0)