Return a Date|Time Stamp
jjweimer
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
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
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
..|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]
December 7, 2009 at 01:39 am - Permalink
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
December 7, 2009 at 07:19 am - Permalink
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:
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.
December 7, 2009 at 09:30 am - Permalink
Thanks.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
December 7, 2009 at 10:06 am - Permalink
SplitString/E=(gExp) secs2time(datetime,2), a, b, c
tstr = a + b + c
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
December 8, 2009 at 08:42 am - Permalink