Return a Date|Time Stamp

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 doesn't get reported.

print DateTimeStamp()
  ..|20:33:50


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 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 following function instead:

Function/S DateTimeStamp()
    String dstr, tstr
   
    Variable timestamp = DateTime
    String sep_char = "."
   
    tstr = Secs2Time(timestamp, 3)
    dstr = Secs2Date(timestamp, -2, sep_char)
   
    // Replace numeric month with text abbreviation.
    String months = "Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec;"
    String month_abbreviation = StringFromList(str2num(StringFromList(1, dstr, sep_char)) - 1, months, ";")
    dstr = RemoveListItem(1, dstr, sep_char)
    dstr = AddListItem(month_abbreviation, dstr, sep_char, 1)
    dstr = RemoveEnding(dstr, sep_char)
    return (dstr + "|" + tstr)
End


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:

Function year()
    return str2num(StringFromList(0, Secs2Date(DateTime, -2), "-"))
End

Function month()
    return str2num(StringFromList(1, Secs2Date(DateTime, -2), "-"))
End

Function day()
    return str2num(StringFromList(2, Secs2Date(DateTime, -2), "-"))
End

Function hour()
    return str2num(StringFromList(0, Secs2Time(DateTime, 3), ":"))
End

Function minute()
    return str2num(StringFromList(1, Secs2Time(DateTime, 3), ":"))
End

Function second()
    return str2num(StringFromList(2, Secs2Time(DateTime, 3), ":"))
End

Function/S GetDateTimeString()
    String dateTimeString = ""
    sprintf dateTimeString, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d\r", year(), month(), day(), hour(), minute(), second()
    return dateTimeString
End


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 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 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.

    gExp = "([0-9]+):([0-9]+):([0-9]+)"
    SplitString/E=(gExp) secs2time(datetime,2), a, b, c
    tstr = a + b + c


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More