# Datetime and duration: new features to create a time vector

**URL:** <https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289>\
**Category:** Data handling\
**Created:** [December 7, 2023, 1:26pm UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289 "2023-12-07T13:26:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adeline.carnis](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@adeline.carnis](https://scilab.discourse.group/u/adeline.carnis)\
**Post date:** [December 7, 2023, 1:26pm UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/1 "2023-12-07T13:26:29Z")

</div>

**Do you know the features allowing to create a time vector and necessary for [timeseries](https://help.scilab.org/timeseries.html)?**

## Datetime

[datetime](https://help.scilab.org/datetime.html) represents a point in time.

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

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

```

or

```auto
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](https://help.scilab.org/duration.html) represents a duration of time in [hours](https://help.scilab.org/hours.html), [minutes](https://help.scilab.org/minutes.html), and [seconds](https://help.scilab.org/seconds.html).

Write `10 hours 35 minutes and 22 seconds` with duration:

```auto
d = duration(10, 35, 22)

```

## Calendarduration

[calendarDuration](https://help.scilab.org/calendarDuration.html) represents a duration of time in years ([calyears](https://help.scilab.org/calyears.html)), months ([calmonths](https://help.scilab.org/calmonths.html)), and days ([caldays](https://help.scilab.org/caldays.html)).

Create a calendarDuration of 3 months

```auto
c = calendarDuration(0, 3, 0)

```

## Mix time types

Create a regular datetime vector with one hour time step:

```auto
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:

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

```

## How many days until Christmas ?

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

```

```auto
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._

---

<div class="post-metadata">

**Author:** ![davcheze](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@davcheze](https://scilab.discourse.group/u/davcheze)\
**Post date:** [January 9, 2024, 3:28pm UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/2 "2024-01-09T15:28:17Z")

</div>

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](https://gitlab.com/scilab/scilab/-/snippets/3637496)) .  
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

---

<div class="post-metadata">

**Author:** ![davcheze](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@davcheze](https://scilab.discourse.group/u/davcheze)\
**Post date:** [January 9, 2024, 4:31pm UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/3 "2024-01-09T16:31:15Z")

</div>

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:

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

```auto
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:

```auto
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 🙂 ! and all these new features are very useful around time management and timeseries

Thank you

David

---

<div class="post-metadata">

**Author:** ![ouaigooo](https://avatars.discourse-cdn.com/v4/letter/o/f07891/32.png) [@ouaigooo](https://scilab.discourse.group/u/ouaigooo)\
**Post date:** [January 15, 2024, 10:17am UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/4 "2024-01-15T10:17:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adeline.carnis](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@adeline.carnis](https://scilab.discourse.group/u/adeline.carnis)\
**Post date:** [January 15, 2024, 11:02am UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/5 "2024-01-15T11:02:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davcheze](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@davcheze](https://scilab.discourse.group/u/davcheze)\
**Post date:** [March 13, 2024, 10:21am UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/6 "2024-03-13T10:21:43Z")

</div>

Hi everyone,

another detail around datetime object may be added in help page, about feature to display or retrieve in variable easily the datetime object as string :

```auto
vt=datetime([2024 01 16 13 57 25.345])
//or eventually even simpler: vt= datetime("2024-01-16 13:57:25.345")
svt= string(vt)

```

This is useful when elaborating legend or labels.

David

---

<div class="post-metadata">

**Author:** ![adeline.carnis](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@adeline.carnis](https://scilab.discourse.group/u/adeline.carnis)\
**Post date:** [March 14, 2024, 10:03am UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/7 "2024-03-14T10:03:14Z")

</div>

Hello David,

Thank you for your feedback. In fact, the datetime help page may be updated with your example.

Adeline

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/pascal/32/40_2.png) [@pascal](https://scilab.discourse.group/u/pascal)\
**Post date:** [July 23, 2025, 11:43am UTC](https://scilab.discourse.group/t/datetime-and-duration-new-features-to-create-a-time-vector/289/8 "2025-07-23T11:43:02Z")

</div>

Hi scilab team,  
i have a question, now that we deal more and more with timeseries. how about change sleep, tic,toc and realtime also into datevec.  
Good or bad idea?  
m.f.G. Pascal
