Return a Date|Time Stamp
Posted December 6th, 2009 by jjweimer
This code snippet shows an example of how to create a date{}time stamp string that can be used for marking events. The optional sep string will go between the values. The returned string is formatted as YYMMDD{sep}HHMM.
Function/S DateTimeStamp([sep]) string sep string a, b, c, dstr, tstr string gExp if (ParamIsDefault(sep)) sep = "" endif gExp = "([0-9]+):([0-9]+)" SplitString/E=(gExp) secs2time(datetime,2), a, b tstr = a + b gExp = "([0-9]+)/([0-9]+)/([0-9]+)" SplitString/E=(gExp) secs2date(datetime,-1), a, b, c dstr = c[2,3] + b + a return (dstr + sep + tstr) end

On my computer the date
On my computer the date doesn't get reported.
For computer logging (not the best visually the best way of doing it) is:
091207203350 OR yearmonthdayhourminutesecond
This is because the time/date stamp can be sorted numerically. Also, since the string is a fixed length the individual parts can be obtained with a substring, i.e.
string year = mydatestring[0,1]
Yes .... caveat emptor (in
Yes .... caveat emptor (in this case "user beware") ... the date() and time() functions return values that are likely platform specific and formatted by what you pre-define in your system preferences. The above works on a Mac with the standard format settings. Certainly, using the datetime() function and converting to the format you recommend would be more appropriate - this task is left as an exercise for the reader :-).
A feature request for Igor Pro is to have functions such as year(), month(), day([inyear/inmonth]), hours([24/12]), minutes(), and seconds() that return respective string or numerical values independent of platform and operating system.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
You could also use the
You could also use the following function instead:
It has the advantage of working using any time/date system settings, though the disadvantage is that the month strings are in English.
For the various year(), month(), etc. functions, you could use these:
I think if we were to improve the ability to format date and time strings, instead of creating new year(), month(), etc. built in functions, it would be better to create a function like PHP's date() function, which, while complicates, allows you to format a date/time in pretty much any way imaginable.
Super! The secs2date
Super! The secs2date function does just the job. Amazing that I missed it. As for the months being in English, substituting with month numbers would take care of that problem.
Thanks.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
I've made changes
I've made changes corresponding to the suggestions. The version shown now should work across platforms and will return a date+time stamp in a YYMMDDHHMM format. To add SS (seconds) to the list, change the lines as shown below.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH