Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

Time.Parse

Parses a date.

Component Version macOS Windows Linux Server iOS SDK
Time 6.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "Time.Parse"; Text; FormatString { ; Locale } )   More

Parameters

Parameter Description Example Flags
Text The text to parse for date and/or time "2016-11-30"
FormatString The format string.
Locale The locale to use.
Be aware that identifiers are not the same on Mac, Windows and Linux.
"FR_fr" Optional

Result

Returns timestamp or error.

Description

Parses a date.
The Time.Parse function parses the string in the parameter, according to the string pointed to by format parameter, and returns the result as timestamp. The resulting values will be relative to the local time zone. Thus, it can be considered the reverse operation of Time.Format.

The Time.Parse function is the converse function to FormateDateMBS and converts the character string pointed to by s to values which are stored in the date, using the format specified by format. Here format is a character string that consists of field descriptors and text characters, reminiscent of scanf (in C++). Each field descriptor consists of a % character followed by another character that specifies the replacement for the field descriptor. All other characters in the format string must have a matching character in the input string, except for whitespace, which matches zero or more whitespace characters in the input string. There should be whitespace or other alphanumeric characters between any two field descriptors.

The Time.Parse function processes the input string from left to right. Each of the three possible input elements (whitespace, literal, or format) are handled one after the other. If the input cannot be matched to the format string the function stops. The remainder of the format and input strings are not processed.

The supported input field descriptors are listed below. In case a text string (such as a weekday or month name) is to be matched, the comparison is case insensitive. In case a number is to be matched, leading zeros are permitted but not required.

%%The % character.
%a or %AThe weekday name according to the current locale, in abbreviated form or the full name.
%b or %B or %hThe month name according to the current locale, in abbreviated form or the full name.
%cThe date and time representation for the current locale.
%CThe century number (0-99).
%d or %eThe day of month (1-31).
%DEquivalent to %m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format is %Y-%m-%d.)
%HThe hour (0-23).
%IThe hour on a 12-hour clock (1-12).
%jThe day number in the year (1-366).
%mThe month number (1-12).
%MThe minute (0-59).
%nArbitrary whitespace.
%pThe locale's equivalent of AM or PM. (Note: there may be none.)
%rThe 12-hour clock time (using the locale's AM or PM). In the POSIX locale equivalent to %I:%M:%S %p. If t_fmt_ampm is empty in the LC_TIME part of the current locale then the behavior is undefined.
%REquivalent to %H:%M.
%SThe second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed).
%tArbitrary whitespace.
%TEquivalent to %H:%M:%S.
%UThe week number with Sunday the first day of the week (0-53). The first Sunday of January is the first day of week 1.
%wThe weekday number (0-6) with Sunday = 0.
%WThe week number with Monday the first day of the week (0-53). The first Monday of January is the first day of week 1.
%xThe date, using the locale's date format.
%XThe time, using the locale's time format.
%yThe year within century (0-99). When a century is not otherwise specified, values in the range 69-99 refer to years in the twentieth century (1969-1999); values in the range 00-68 refer to years in the twenty-first century (2000-2068).
%YThe year, including century (for example, 1991).

Some field descriptors can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used. If the alternative format or specification does not exist in the current locale, the unmodified field descriptor is used.
The E modifier specifies that the input string may contain alternative locale-dependent versions of the date and time representation:

%EcThe locale's alternative date and time representation.
%ECThe name of the base year (period) in the locale's alternative representation.
%ExThe locale's alternative date representation.
%EXThe locale's alternative time representation.
%EyThe offset from %EC (year only) in the locale's alternative representation.
%EYThe full alternative year representation. The O modifier specifies that the numerical input may be in an alternative locale-dependent format:
%Od or %OeThe day of the month using the locale's alternative numeric symbols; leading zeros are permitted but not required.
%OHThe hour (24-hour clock) using the locale's alternative numeric symbols.
%OIThe hour (12-hour clock) using the locale's alternative numeric symbols.
%OmThe month using the locale's alternative numeric symbols.
%OMThe minutes using the locale's alternative numeric symbols.
%OSThe seconds using the locale's alternative numeric symbols.
%OUThe week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols.
%OwThe number of the weekday (Sunday=0) using the locale's alternative numeric symbols.
%OWThe week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.
%OyThe year (offset from %C) using the locale's alternative numeric symbols.

Returns true on success. If the functions fails to match all of the format string and therefore an error occurred the function returns false.

Before libc 5.4.13 whitespace (and the 'n' and 't' specifications) was not handled, no 'E' and 'O' locale modifier characters were accepted, and the 'C' specification was a synonym for the 'c' specification.

The 'y' (year in century) specification is taken to specify a year in the 20th century by libc4 and libc5. It is taken to be a year in the range 1950-2049 by glibc 2.0. It is taken to be a year in 1969-2068 since glibc 2.1.

For reasons of symmetry, glibc tries to support for ParseDateMBS the same format characters as for FormatDateMBS. (In most cases the corresponding fields are parsed, but no field in tm is changed.) This leads to
%FEquivalent to %Y-%m-%d, the ISO 8601 date format.
%gThe year corresponding to the ISO week number, but without the century (0-99).
%GThe year corresponding to the ISO week number. (For example, 1991.)
%uThe day of the week as a decimal number (1-7, where Monday = 1).
%VThe ISO 8601:1988 week number as a decimal number (1-53). If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1.
%zAn RFC-822/ISO 8601 standard timezone specification.
%ZThe timezone name.

Similarly, because of GNU extensions to FormatDateMBS, %k is accepted as a synonym for %H, and %l should be accepted as a synonym for %I, and %P is accepted as a synonym for %p. Finally
%sThe number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). Leap seconds are not counted unless leap second support is available.

The glibc implementation does not require whitespace between two field descriptors.

Examples

Parse a date:

MBS( "Time.Parse"; "2016-11-30"; "%Y-%m-%d" )

Example result: 30.11.2016 00:00

Parse a timestamp:

MBS( "Time.Parse"; "2016-11-30 13:31"; "%Y-%m-%d %H:%M" )

Example result: 30.11.2016 13:31

See also

Release notes

  • Version 14.0

Blog Entries

This function is free to use.

Created 17th August 2016, last changed 14th November 2023


Time.Format - Time.Sleep