Purpose
Returns a DateTime value given a string that represents a date and time. You can call this function from the MapBasic window in MapInfo Pro.
Syntax
StringToDateTime( "dateTimeString", "dateTimeFormat", "locale" )
dateTimeString is a string expression representing a datetime. You can use a sequence of 17 digits:
- 4 for the year
- 2 each for the month, day, hour, minutes, and seconds
- 3 for milliseconds
The sequence can be entered as a string in quotes or a number. If entering fewer than 17 digits, then a string and number are interpreted differently. A string has zeroes added to the right and a number has zeroes added to the left: "20150909" would be evaluated as the string "20150909000000000" while 20150909 would be evaluated as the number 00000000020150909.
dateFormat is an optional string expression representing the date format to apply (overriding the system date-time format ).
locale is an optional string expression representing the locale of the date-time format applied.
Return Value
Description
MapBasic interprets the date/time string according to the date and time-formatting options that are set up on the computer (see DateTime). At least one space must be between the date and time. See StringToDate() function and StringToTime() function for details.
You can also use the optional dateFormat and locale arguments to explicitly apply date formatting conventions.
Example
This examples uses a United States time format where colons separate hours (hh), minutes (mm), seconds (ss), and fractions (fff) (for details, see Time or FormatTime$() function),
dim strX as string
dim Z as datetime
strX = "1999:09:25:12:32:45"
Z = StringtoDateTime(strX)
Print FormatDate$(Z)
Print FormatTime$(Z,"hh:mm:ss.fff tt")
Short DateTime format in the US (AM or PM):
Print StringToDateTime("06.04.2021 3:20:35 PM","dd.MM.yyyy h:mm:ss tt", "en-US")
//returns: 20210406152035000
Long DateTime format in the US (AM or PM):
Print StringToDateTime ("Monday, June 15, 2009 12:09:25 PM","dddd, MMMM d, yyyy h:mm:ss tt", "en-US")
//returns: 20090615120925000
Short DateTime format in Germany (24 Hour clock):
Print StringToDateTime ("15.04.2021 15:20:35","dd.MM.yyyy HH:mm:ss", "de-DE")
//returns: 20210415152035000
Long DateTime format in Germany:
Print StringToDateTime ("Montag, 15. Juni 2009","dddd, dd. MMMM yyyy", "de-DE")
//returns: 20090615
Long DateTime format in Turkey (24 Hour clock):
Print StringToDateTime ("Pazartesi, haziran 15, 2009 15:20:35","dddd, MMMM d, yyyy HH:mm:ss", "tr-TR")
//returns: 20090615152035000
Long DateTime format in Russia (24 Hour clock):
Print StringToDateTime ("19 ноября 2018 r. 15:20:35","d MMMM yyyy r. H:mm:ss", "ru-RU")
//returns: 20181119152035000
See Also:
Date and Time Functions, NumberToDateTime() function, Set Format statement, Str$() function, StringToDate() function, StringToTime() function