I am trying to convert the following datetime into string format in Matlab.
20121003.03
It corresponds to 3rd Oct 2012, 03:00am. Any ideas?
You can specify a custom input format when creating a datetime object. Your question seems to indicate that the object is already a datetime object, but at that point the problem is trivial, just call datestr on it (the last line of the script below). I'll assume here that the input is a floating point representation.
dval = 20121030.0330;
% Convert floating point number to a string
dstr = sprintf('%013.04f', dval);
% Convert string to a datetime object
dtime = datetime(dstr, 'InputFormat', 'yyyyMMdd.hhmm');
% Convert the datetime object into a formatted string
dstr2 = datestr(dtime);
Output:
dstr2 = '30-Oct-2012 03:00:00'
If you want an output format different than the default then you need to specify the output format of datestr. See the documentation for more information.
Related
From reading code from elsewhere, I have a matrix of dates called 'time' that have unwanted spaces that I want removed.
I've tried isspace and regexprep with no luck
time = regexprep(time, '\W', '');
I have about 130000 dates with the following format:
04-July -2017 09:54:30.000
04-July -2017 09:54:31.000
etc
There are two spaces between the end of 'July' to the next dash I want to suppress to:
04-July-2017 09:54:30.000
04-July-2017 09:54:31.000
Replace two or more spaces with nothing:
>> time = {'04-July -2017 09:54:30.000'
'04-July -2017 09:54:31.000'}
>> regexprep(time,' {2,}','')
{'04-July-2017 09:54:30.000'}
{'04-July-2017 09:54:31.000'}
Unless you just want to correct your input file for later usage, you do not necessarily need to correct the input. There are several ways to parse the time directly with the extra spaces:
Let time be:
time = ['04-July -2017 09:54:31.000';
'04-July -2017 09:54:32.000']
Then to directly parse the string representation of the datetime into a MATLAB date serial number you can use:
%% get date in [MATLAB date serial number]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
matlabTime = datenum(time,formatIn)
matlabTime =
736880.412858796
736880.41287037
This serial time representation is not so human readable but it's the fastest thing you can get if you want to do calculations with date/time.
If your goal is simply to correct the string, then you can use the same trick to read the value in, and define exactly which output format you want out:
%% get date in [string]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
formatOut = 'dd-mmm-yyyy HH:MM:SS.FFF' ;
stringTime = datestr(datenum(time,formatIn),formatOut)
stringTime =
04-Jul-2017 09:54:31.000
04-Jul-2017 09:54:32.000
If you want to use the new datetime objects, the input format has a slight different syntax but the operation is roughly the same:
%% get date in [datetime] objects
formatIn = 'dd-MMM -yyyy HH:mm:ss.SSS' ;
t = datetime(time,'InputFormat',formatIn)
t =
04-Jul-2017 09:54:31
04-Jul-2017 09:54:32
Although the MATLAB console display t in human readable format, t is now a datetime object. Check the documentation if you want to use this.
Replace only two white-spaces after a month and preceding a dash (-):
>> date = '04-July -2017 09:54:30.000';
>> regexprep(date, '(\w) -', '$1-')
ans =
'04-July-2017 09:54:30.000'
I am having an error my date format is
DateSTr{1} {'30/07/2017 12:00:00'} Datestr{2} {'31/07/2017'}
The format of DateStr{2} is wrong. How can I make sure it is at the good format?
Error using datenum (line 190)
DATENUM failed.
Error in ModemJobAnalysis/GetReceptionSuccessProbability>GetEventMask (line 107)
Dates=[datenum(DateStr{1},date_format),datenum(DateStr{2},date_format)];
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.
You are using the same date_format for both strings, you should change your format accordingly the input string as in example below:
If in your input string you have 30/07/2017 12:00:00, your date_format should be 'dd/mm/yyyy HH:MM:SS':
>> datenum('30/07/2017 12:00:00','dd/mm/yyyy HH:MM:SS')
ans =
7.3691e+05
But when you don't use time (like your second string 31/07/2017) , you should change your format to 'dd/mm/yyyy':
>> datenum('31/07/2017','dd/mm/yyyy')
ans =
736907
Therefore, to correct your code try something like:
Dates=[datenum(DateStr{1},'dd/mm/yyyy HH:MM:SS'),datenum(DateStr{2},'dd/mm/yyyy')];
EDIT
You can verify the size of your string to see if it contains the time using length(DateSTr{1}) which gives 10 to 31/07/2017 and 19 to 30/07/2017 12:00:00.
Therefore, to adjust all your strings, just verify its size, and add the time marker at the end, if necessary, such as:
DateSTr = {'30/07/2017 12:00:00', '31/07/2017'}
for k=1:length(DateSTr)
if length(DateSTr{k}) < 19 % If date string is not complete
DateSTr{k} = strcat(DateSTr{k},' 00:00:00') % Concatenate time to correct the string
end
end
Which gives, respectively, input and output:
DateSTr =
1×2 cell array
{'30/07/2017 12:00:00'} {'31/07/2017'}
DateSTr =
1×2 cell array
{'30/07/2017 12:00:00'} {'31/07/2017 00:00:00'}
I have a numeric vector corresponding to dates in the following format yyyymmdd, ie for December 24th, 2010 it is 20101224. How can I convert it into text format, i.e. in the following format 'mm-dd-yyyy'?
You should really use datetime rather than convert to strings,
dates = datetime(20100124,'ConvertFrom','yyyymmdd')
The first input can be a numeric vector, assuming it's of the yyyymmdd format.
If you then want to specify a display format use,
dates.Format = 'MM-dd-yyyy'
If you really need them as strings you can then use,
dates = datestr(dates)
Matlab has a datestr command which might be useful. Example usage:
formatOut = 'mm-dd-yyyy';
datestr(now,formatOut)
For your date, you could convert the input number to a string, convert the string to a date and create a date string with the new format.
formatIn = 'yyyymmdd';
formatOut = 'mm-dd-yyyy';
inStr = num2str(20101224); % Skip this step if already a number
outStr = datestr(datenum(inStr, formatIn), formatOut)
Very simple question. I'm using Matlab's datetime type, so I can carry timezone information. I need to get a specific string representation, to input into a DB. But datestr() does not have any fields to output tz info.
a = datetime('now', 'TimeZone', 'UTC');
%need output in the format 'YYYYMMDDTHH:MM:SS+00:00'
Any thoughts?
You can get the output you want by setting the Format property of the datetime object to display the time zone offset, converting it to a character array, then replacing the space by 'T':
>> a = datetime('now', 'TimeZone', 'UTC', 'Format', 'yyyyMMdd HH:mm:SSxxxxx')
a =
datetime
20171002 21:37:74+00:00
>> out = strrep(char(a), ' ', 'T')
out =
20171002T21:37:74+00:00
Also, take note of the case of the letters in the format string, as that matters for some of them.
I also tried the following:
datestr('19-01-2004','dd-mm-yyyy')
ans =
26-06-0024
I am new to MATLAB, so I am not sure what else to check.
In the function datestr(), the 2nd parameter denotes how the output should look like. It doesn't say anything about the input.
Essentially, you try to perform 2 steps: parse a string and then format the parsed date again.
So you can do
n = datenum('19-01-2004','dd-mm-yyyy')
datestr(n, 'yyyy-mm-dd')
and you'll get an n of 731965 and a final output of 2004-01-19.
You can as well do
v = datevec('19-01-2004','dd-mm-yyyy')
datestr(v, 'yyyy-mm-dd')
and your v becomes [2004 1 19 0 0 0].
So remember: step 1 - parsing of input with the appropriate format string, step 2 - formatting of output with the wanted format string.
If you want to give the date in a "clean" and readable format, you could just do
v = [2004 1 19 0 0 0]
datestr(v, 'yyyy-mm-dd')
datestr(v, 'dd.mm.yyyy')
datestr(v, 'mm/dd/yyyy')
When using datestr to convert a date string from one form to another, the format of the input date string is limit to those listed here. The format of your input '19-01-2004' is 'dd-mm-yyyy' and is not one of the supported formats.
If we change the input string to '01/19/2004', which is the supported format 'mm/dd/yyyy', we get the correct output:
>> datestr('01/19/2004','dd-mm-yyyy')
ans =
19-01-2004
To circumvent the limited number of supported input formats, the documentation recommends using datenum first. So you can map your original input onto itself like:
>> datestr(datenum('19-01-2004','dd-mm-yyyy'),'dd-mm-yyyy')
ans =
19-01-2004
As for why MATLAB returns the date it does has to do with how it handles the unknown format.
I suspect whatever method they use to finally decide upon a format results in a really small date number, hence the year 24 output.