how to use datestr?

31 views (last 30 days)
Teklehaimanot
Teklehaimanot on 23 May 2015
Commented: Walter Roberson on 3 Apr 2019
Dear all, I tried to convert the matlab serial dates (vector) back to a date string 'yyy-MM-dd-HH-mm using the following command. I use matlab2013a. time_num is a vector of serial dates. I have attached the file please.
time_format=datestr(datetime(time_num/1440,'ConvertFrom','datenum','Format','yyyy-MM-dd HH:mm')); %saves new time in date format
Undefined function 'datetime' for input arguments of type 'double'.

Accepted Answer

per isakson
per isakson on 23 May 2015
Edited: per isakson on 23 May 2015
With R2013a this is the way
>> vec = datevec( now )
vec =
1.0e+03 *
2.0150 0.0050 0.0230 0.0070 0.0590 0.0434
>> datestr( vec, 'yyyy-mm-dd HH:MM' )
ans =
2015-05-23 07:59
>>
I assume this still works in R2015a. However, in R2015a there are new features related to date and time. I guess, you are trying to use these new features. Which release do you use?
  3 Comments
per isakson
per isakson on 23 May 2015
datetime was Introduced in R2014b
Walter Roberson
Walter Roberson on 3 Apr 2019
nashwan sebi comments to per:
great answer, it works great thanks

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 23 May 2015
Edited: Stephen23 on 23 May 2015
The serial date numbers in your time_num.mat file can be converted into strings like this:
>> datestr(time_num(1:10), 'yyyy-mm-dd HH:MM')
ans =
2013-12-20 00:00
2013-12-20 00:10
2013-12-20 00:20
2013-12-20 00:30
2013-12-20 00:40
2013-12-20 00:50
2013-12-20 01:00
2013-12-20 01:10
2013-12-20 01:20
2013-12-20 01:30
Read the datestr documentation to know how this works, and also to pick the correct output format string.
  2 Comments
Teklehaimanot
Teklehaimanot on 23 May 2015
Thank you Stephen,yes this is a simple way of coverting.
Teklehaimanot
Teklehaimanot on 23 May 2015
I tried and it works well. Thank you again for your constant help!

Sign in to comment.


Rajesh PolamReddy
Rajesh PolamReddy on 7 Aug 2018
I need to convert the vector Tstamp to HH:MM:SS format
datestr is converting the vector to time of string but i don't see the converted data is correct
eg : Tstamp = [4696.9,4702.9,4708.9];
datestr(x,'HH:MM:SS PM')
I got answer as below
9:36:00 PM
9:36:00 PM
9:36:00 PM
If we see the Tstamp (1) i.e 4696.9 sec is not equal to 9:36:00 PM
Can any one help me ? Iam i missing something?
  1 Comment
Stephen23
Stephen23 on 7 Aug 2018
Edited: Stephen23 on 7 Aug 2018
"If we see the Tstamp (1) i.e 4696.9 sec is not equal to 9:36:00 PM"
Actually it is a correct conversion, assuming that the input is a serial date number:
>> T = [4696.9,4702.9,4708.9];
>> datevec(T(:))
ans =
12 11 8 21 36 0
12 11 14 21 36 0
12 11 20 21 36 0
(but note that your values are not serial date numbers: this is your mistake).
"Can any one help me ?"
The MATLAB documentation can help you: "...represents each point in time as the number of days from January 0, 0000. The numeric values also can represent elapsed time in units of days"
"Iam i missing something?"
You assumed that MATLAB serial date numbers are seconds from some epoch, whereas in fact the documentation clearly describes them as being days from the 0th of January in year 0. The datestr documentation clearly describes its input as being a DateVector or DateNumber (what you used, and is measured in days).
You could either:
  • make the conversion yourself (seconds to days is trivial).
  • use a datetime object or duration object to store that data.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!