Variables and direct data inputs tend to fall into various buckets based on their data type. Integer, decimal or float (or “real”), dates and times, strings, bits, blobs, and compound types like objects, arrays, dictionaries, hashes and lists, and so on.
Since this can be a HUGE (say HYOOOOOOOOOOOOOOOOOGE) topic, I will need to trim off some of the fat and keep it manageable. So let’s pick two:
These are typically obtained from one of two sources: files and human input. Files will often contain dates and times as string values, because they often contain nothing but text. This is particularly true for log files…
20150418083002.004 [info] class instance was created.
In this example, the date string is “20150418083002.004”, which is in the form of YYYYMMDDHHMMSS.NNN where NNN is the GMT offset code. Easy enough. In this example, you just parse the value up into neat little chunks and eat them with a fork.
Some may also use a more traditional format, such as:
2015-04-18 08:30:02 info: class instance was created.
Here’s where it gets interesting. Some languages have more “built-in” features to handle situations that in other languages require custom code. If you want convert this into a date value which can be used for date manipulation functions, such as comparing dates to determine differentials (how many days between date1 and date2, etc.), or producing a formatted result (e.g. “April 18, 2015”), then this can go in several directions, again, depending upon the language being used.
d1 = "20150418083002.004" d2 = FormatDateTime(_ Left(d1,4) & "-" & _ Mid(d1,5,2) & "-" & _ Mid(d1,7,2), vbShortDate) ' results in 4/18/2015 d1 = "2015-04-18 08:30:02" d2 = FormatDateTime(d1, vbShortDate) ' results in 4/18/2015
var d1 = "20150418083002.004"; var yy = d1.substring(0,4); var mm = d1.substring(4,6); var dd = d1.substring(6,8); var d2 = new Date(mm + '/' + dd + '/' + yy); // results in "Sat Apr 18 2015 20:30:02 GMT-0400 (Eastern Daylight Time)
$d1 = "20150418083002.004" $d0 = $d1.Substring(4,2) + "/" + $d1.Substring(6,2) + "/" + $d1.Substring(0,4) $d2 = [datetime]$d0 # results in "Saturday, April 18, 2015 12:00:00 AM" $d1 = "2015-04-18 08:30:02" $d2 = [datetime]$d1 # results in "Saturday, April 18, 2015 8:30:02 AM"
(setq d1 "20150418083002.004" d2 (strcat (substr d1 5 2) "/" (substr d1 7 2) "/" (substr d1 1 4) ) ) ' results in "04/18/2015" ' there are no explicit (date) or (datetime) functions in AutoLISP or Visual LISP unfortunately. This is likely due to the focus of the language: 2D and 3D design manipulation.
So, as you can see, when it comes to dates and times (or date-time values), the level of capability you can leverage from the built-in environment depends upon the form of the input. If the input is fairly standard and “expected”, such as “4/18/2015” or “2015-04-18”, then you can make it work with very little additional code (if any). When the date string is non-standard, you often need to break out the string manipulation features to make things work.