Shahgee
Shahgee

Reputation: 3405

Getting datenum of predefined days, Matlab

I can get datenum of today

Daynum=datenum(fix(clock))

But how can I get it

  1. yesterday

  2. 7 days earlier

  3. 6-11-2015

    I need all three above listed days. I know some of these are already integrated in new version but I don't have Matlab 2015.

Upvotes: 1

Views: 37

Answers (1)

sco1
sco1

Reputation: 12214

The output of datenum is:

A serial date number represents the whole and fractional number of days from a fixed, preset date (January 0, 0000).

So to get yesterday you could do:

Daynum_yesterday = datenum(fix(clock)) - 1;

And 7 days ago would be:

Daynum_7days = datenum(fix(clock)) - 7;

If you have a specific date you can already pass it to datenum, with an optional format specifier:

Daynum_mydate = datenum('6-11-2015');
% or
Daynum_mydate = datenum('6-11-2015', 'mm-dd-yyyy');

Which return the same result.

We can test all of these using datestr:

str_yesterday = datestr(Daynum_yesterday);
str_7days = datestr(Daynum_7days);
str_mydate = datestr(Daynum_mydate);

Which returns:

str_yesterday =

16-Nov-2015 07:44:41


str_7days =

10-Nov-2015 07:44:41


str_mydate =

11-Jun-2015

Edit: And an obligatory Falsehoods Programmers Believe About Time citation.

Upvotes: 4

Related Questions