Purpose
Returns a Date value, given a string. You can call this function from the MapBasic window in MapInfo Pro.
Syntax
StringToDate( "dateString", "dateFormat", "locale" )
dateString is a string expression representing a date.
dateFormat is an optional string expression representing the date format to apply (overriding the system date format ).
locale is an optional string expression representing the locale of the date format applied.
Return Value
Description
The StringToDate() function returns a Date value, given a string that represents a date. MapBasic interprets the date string according to the date-formatting options that are set up on your computer. Computers are often configured as Day/Month/Year and may use a period (.) or backslash (/) for the separator character. Computers within the United States are configured to format dates as Month/Day/Year. To force the StringToDate() function to apply United States formatting conventions, use the Set Format statement.
You can also use the optional dateFormat and locale arguments to explicitly apply date formatting conventions.
The datestring argument must indicate the month (1-12, represented as one or two digits) and the day of the month (1-31, represented as one or two digits). You can specify the year as a four-digit number or as a two-digit number, or you can omit the year entirely. If you do not specify a year, MapInfo Pro uses the current year. If you specify the year as a two-digit number (for example, 96), MapInfo Pro uses the current century or the century as determined by the Set Date Window() statement.
Example
Short Date format in US:Print StringToDate("2009.06.15","yyyy.MM.dd", "en-US")
//returns: 20090615
Long Date format in US:
Print StringToDate("Monday, June 15, 2009","dddd, MMMM d, yyyy", "en-US")
//returns: 20090615
Month Day format in US:
Print StringToDate("June 15","MMMM dd", "en-US")
//returns: 20210615 ‘in current year
Year Month format in US:
Print StringToDate("June 2009","Y", "en-US")
//returns: 20090615
Short Date format in Germany:
Print StringToDate("15.04.2021","dd.MM.yyyy", "de-DE")
//returns: 20210415
Long Date format in Germany:
Print StringToDate("Montag, 15. Juni 2009","dddd, dd. MMMM yyyy", "de-DE")
//returns: 20090615
Short Date format in Japan:
Print StringToDate("2019年01月24日","yyyy年MM月dd日", "ja-JP")
//returns: 20190124
Short Date format in Russia:
Print StringToDate("15.04.2021"," dd.MM.yyyy ", "ru-RU")
//returns: 20210415
Long Date format in Russia:
Print StringToDate("19 ноября 2018 r. ","d d MMMM yyyy r. ", "ru-RU"
//returns: 20181119
The following example specifies date strings with U.S. formatting: Month/Day/Year. Before calling StringToDate(), this program calls the Set Format statement to guarantee that the U.S. date strings are interpreted correctly, regardless of how the system is configured.
Dim d_start, d_end As Date
Set Format Date "US"
d_start = StringToDate("12/17/92")
d_end = StringToDate("01/02/1995")
Set Format Date "Local"
In this example, the variable Date1 = 19890120, Date2 = 20101203 and MyYear = 1990.
DIM Date1, Date2 as Date
DIM MyYear As Integer
Set Format Date "US"
Set Date Window 75
Date1 = StringToDate("1/20/89")
Date2 = StringToDate("12/3/10")
MyYear = Year("12/30/90")
These results are due to the Set Date Window() statement which allows you to control the century value when given a two-digit year.
See Also:
Date and Time Functions, NumberToDate() function, NumberToDateTime() function, Set Format statement, Str$() function, StringToDateTime() function, StringToTime() function