Eric Leschinski
Eric Leschinski

Reputation: 154063

In GNU Octave, get the date and time

I want to get the date and time in GNU Octave and print it to screen. What is the best way to do this?

I've checked the Octave documentation, but I don't see a function to print out YYYY-MM-DD HH:MM:SS with a one liner simple command.

http://www.gnu.org/software/octave/doc/interpreter/Timing-Utilities.html

Upvotes: 5

Views: 13684

Answers (1)

Eric Leschinski
Eric Leschinski

Reputation: 154063

Get the date and time in Octave:

Number of seconds since epoch as integer:

time()
ans = 1.3482e+09

The date(), now() and datestr(...) builtin methods:

date()
ans = 20-Sep-2012

datestr(now(), 'yyyy-mm-dd');
ans = 2022-07-04

str2num(datestr(now(), 'yyyymmddHHMMSS'))
ans =  20220704165727

Add/Subtract days/months interval from now:

datestr(addtodate(now(), -20, 'days'), 'yyyy-mm-dd')
ans = 2022-06-14
datestr(addtodate(now(), -20, 'month'), 'yyyy-mm-dd')
ans = 2020-11-04

Format date/time as string with strftime

strftime ("%r (%Z) %A %e %B %Y", localtime (time ()))
ans = 09:52:42 PM (EDT) Thursday 20 September 2012

Get yyyy-mm-dd hh:mm:ss format

strftime ("%Y-%m-%d %H:%M:%S", localtime (time ()))
ans = 2012-09-20 21:56:07

Sources:

https://octave.sourceforge.io/octave/function/datestr.html

https://octave.sourceforge.io/octave/function/strftime.html

Upvotes: 7

Related Questions