Datetime and duration: new features to create a time vector

Do you know the features allowing to create a time vector and necessary for timeseries?

Datetime

datetime represents a point in time.

How to write this date and time November 29, 2023 at 10:05:23 with datetime ?

dt = datetime(2023, 11, 29, 10, 5, 23)

or

dt = datetime("November 29, 2023 10:05:23", "InputFormat", "MMMM dd, yyyy HH:mm:ss")

Note that datetime() returns the current date and time.

Duration

duration represents a duration of time in hours, minutes, and seconds.

Write 10 hours 35 minutes and 22 seconds with duration:

d = duration(10, 35, 22)

Calendarduration

calendarDuration represents a duration of time in years (calyears), months (calmonths), and days (caldays).

Create a calendarDuration of 3 months

c = calendarDuration(0, 3, 0)

Mix time types

Create a regular datetime vector with one hour time step:

dt1 = datetime(2023, 10, 25, 10, 5, 23)
dt2 = datetime(2023, 10, 25, 15, 30, 0)
dt = dt1:hours(1):dt2

For a regular datetime vector with three months time step:

dt = datetime(2023, 10, 25):calmonths(3):datetime(2024, 3, 31)

How many days until Christmas ?

d1 = datetime(2023, 12, 25)
d2 = datetime("today")
d = d1 - d2
d = days(d)

close(winsid())
d1 = datetime(2023, 12, 25);
d2 = datetime("today");
if d1 < d2 then
    disp("xmas is over!");
    return
end
d = d1 - d2;
d = days(d);

str = "J - " + string(d);

f = figure("figure_name","Christmas countdown", ...
"background", color(187,11,11), ...
"figure_position", [928, 305], ...
"axes_size", [630 670], ...
"visible", "off", ...
"menubar_visible", "off", ...
"toolbar_visible", "off", ...
"infobar_visible", "off", ...
"icon", "scilab", ...
"resize", "off", ...
"dockable", "off");

x = [0 6 5 5.5 4.5 5 4 4.5 3.5 4 3 2 2.5 1.5 2 1 1.5 0.5 1 0];
y = [0 0 1.5 1.5 3 3 4.5 4.5 6 6 8 6 6 4.5 4.5 3 3 1.5 1.5 0];
xfpoly(x, y, color(9,82,40))
p = scatter(3, 8, 2500, color(212, 175, 55), "p", "markerBackground", color(212,175,55));
xstring(1.7, 0, string(d2));
x = gce();
x.font_size = 6;
x.font_style = 2;
x.font_foreground = color(187,11,11);
xstring(1.5, 1, str);
x = gce();
x.font_size = 11;
x.font_style = 2;
x.font_foreground = color(187,11,11);
xfpoly([2.5 3.5 3.5 2.5 2.5], [-1 -1 0 0 -1], color(88, 41, 0));

a = gca();
a.data_bounds = [-0.5 -1.5;6.5 10];
title("Xmas is coming!!", "fontsize", 5, "fontname", 2, "color", color(255,255,255), "position", [1.5 10.5]);
plot([0 0.5 1 1.5 2 4 4.5 5 5.5 6], [0 1.5 3 4.5 6 6 4.5 3 1.5 0], "o", "markerFaceColor", color(140, 10, 10), "markerEdgeColor", color(140, 10, 10), "markerSize", 11);

c = rand(1000, 1)*8 - 1;
b = rand(1000, 1)*12 - 2;
scatter(c(1:200), b(1:200), 50, color(212, 175, 55), "*")

a.axes_visible = "off";
a.filled = "on";
a.background = color(255,255,255);
e = gce();
e = e.children;
f.visible = "on";

for i = 2:5
    idx = 200*(i-1)+1:200*i;
    data = [c(idx), b(idx)];
    for j = 1:200
        e.data(j,:) = data(j,:);
        sleep(10)
    end
end

As usual, don’t hesitate to share with us your use cases and issues.

2 Likes

Hi Adeline,
I wonder how I could convert datetime variable (type is 17) into former underlaying datenum variable (type is 1) ?
Indeed i mixing in current program some timeseries object with new datetime type with former developments that were using datenum type representation of time, and i need to synchronize data from both sources into existing graphic representation relaying on datenum time reference (user function labxdtv) .
I looked into help page for datatime, i can convert from datenum to datetime but reciprocal way is not mentionned. Is there a workaround to get underlying datenum values?
Currently I just managed to convert datetime to string type variable through string(datetimevar) function. I failed to convert to datenum when using double(datetimevar) function.
Thank you for any clue,

David

I just found the answer, thanks to object treeview of the datetime object: we can get the datenum value(s) corresponding to datetime through the date properties of datetime variables:

vt=datetime([2024 01 9])
vt.date

However it’s returning datenum integer value for the day of the year, not the real value corresponding to eventual time property of the datetime object.

vt=datetime([2024 01 16 13 57 25.345])
vt.date
vt.time
fulldatenum= vt.date+vt.time/(3600*24)  //conversion to datenum
datevec(fulldatenum)  // retrieve numerical values 
datetime(fulldatenum,"ConvertFrom","datenum")

It would be nice to have the reverse function or property with fulldatenum value.

It’s worth noticing that for duration object , we can retrieve the duration in real format in milliseconds through the duration property:

trem= datetime([2024 01 09 11 08 27; 2024 01 10 7 55 0])
d=trem(2)-trem(1)  // duration object
d.duration /1000  // conversion for number of seconds between the datetime entries

These details about object properties and access may be added in the respective help pages.

it’s fine :slight_smile: ! and all these new features are very useful around time management and timeseries

Thank you

David

1 Like

hi,
How about timezones ?
Also, is the datetime created in UTC or local time ?
What happens for 2023-10-29 02:00 - 03:00 local time, and 2023-03-26 01:00-03:00 ?
Thanks

Hello,
datetime does not manage the timezone and the winter and summer time change.
datetime is based on datenum which converts date vectors into serial date numbers. Date numbers are serial days elapsed from some reference date (1-Jan-0000).
Adeline