Converting non standard date using datenum - matlab

I am trying to use datenum to get a datenumber for a set of dates from a cell. Automatic datenum works but it only gets times correctly using conversion, and not days.
The date format is like this: yyyy-mm-dd hh:mm:ss.000
And I have tried using
X_time(:,1) = datenum(Y_time, 'dd/mm/yyyy HH:MM:SS')
to no avail:
Error using datenum (line 178)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.

Your format string for datenum must exactly match the format of the string you're inputting, and no field can be specified more than once. When defining datenum format strings, it is therefore important to differentiate between MM (minutes) and mm (month).
So yyyy-mm-dd hh:mm:ss.000 becomes yyyy-mm-dd HH:MM:SS.FFF
While months and minutes are the only ones that have this overlap problem, it is standard to use lower case for dates and upper case for times in the format string.

Related

How to transform date in Stata?

I've looked for help on the internet for the following, but I could not find a satisfying answer: for an assignment, I need to plot the time series of a certain variable (the term spread in percentages), with years on the x-axis.
However, we use daily data. Does anybody know a convenient way in which this can be done? The 'date' variable that I've got is formulated in the following way: 20111017 represents the 17th of October 2011.
I tried to extract the first 4 numbers of the variable 'date', by using the substr(date, 1, 4) command, but the message 'type mismatch' popped up. Also, I'm not quite sure if it gives the right information if I only use the years to plot daily data (over the years). It now gives the following graph, which doesn't look that nice.
Answering the question in your title.
The date() function expects a string. If your variable with value 20111017 is in a numeric format you can convert it like this: tostring datenum , gen(datestr).
Then when using the date() function you must provide a mask that tells Stata what format the date string is in. Below is a reproducible example you can run to see how this works.
* Example generated by -dataex-. For more info, type help dataex
clear
input float datenum
20111016
end
* Convert numberic varaible to string
tostring datenum , gen(datestr)
* Convert string to date
gen date = date(datestr, "YMD")
* Display date as date
format date %td
If this does not help you, try to provide a reproducible example.
This adds some details to the helpful answer by #TheIceBear.
As he indicates, one way to get a Stata daily date from your run-together date variable is convert it to a string first. But tostring is just one way to do that and not essential. (I have nothing against tostring, as its original author, but it is better suited to other tasks.)
Here I use daily() not date(): the results are identical, but it's a good idea to use daily(): date() is all too often misunderstood as a generic date function, whereas all it does is produce daily dates (or missings).
To get a numeric year variable, just divide by 10000 and round down. You could convert to a string, extract the first 4 characters, and then convert to numeric, but that's more operations.
clear
set obs 1
gen long date = 20111017
format date %8.0f
gen ddate = daily(strofreal(date, "%8.0f"), "YMD")
format %td ddate
gen year = floor(date/10000)
list
+-----------------------------+
| date ddate year |
|-----------------------------|
1. | 20111017 17oct2011 2011 |
+-----------------------------+

Azure Data factory not able to convert date format

I'm trying to get DAYID as string in format YYYYMMDD, however its not working correctly.
I have a timestamp field, I take first 10 characters and convert it into date (working correctly)
toDate(substring(EventTimestamp,1,10))
-- 2021-03-24
However when I try to convert to string using below expression, I;m getting wrong answer. I;m getting the Day as 83.
toString(toDate(substring(EventTimestamp,1,10)),'YYYYMMDD')
--20210383
Date format characters are case-sensitive as per this answer here, so use yyyyMMdd instead.
An example using formatDateTime:
#formatDateTime(utcnow(), 'yyyyMMdd')

Matlab - how to convert date format in example provided

In my code below I am unable to convert the date format in column 1 from dd/MM/yyyy to MM-dd-yyyy. How do I configure it correctly to stop seeing this error??
>> T = readtable("IGEv3.xlsx");
>> tday=T{2:end, 1};
>> tday=datestr(datenum(tday, 'dd-MM-yyyy'), 'MM-dd-yyyy')
Error using datetime/datenum
Too many input arguments.
That looks like your Date column contains datetime values instead of strings or datenum doubles. (Strings or cellstrs show up with quotes in table display output. And the error message "Error using datetime/datenum" means that you're calling datenum() on a datetime input.)
datetime values support datestr calls directly. You can probably just do this:
tday=datestr(T{2:end,1}, 'mm-dd-yyyy')
And personally, I find it more readable when you reference variables by name instead of index:
tday = datestr(T.Date(2:end), 'mm-dd-yyyy')

Matlab and excel timestamp with datenum

At the moment I am using xlsread to open a set of data that I have in excel with given timestamps. But when these values are placed in matlab it changes the formatting of the timestamp.
In excel it is:
dd/mm/yyyy HH:MM
but when it puts it into matlab it changes it to
mm/dd/yyyy HH:MM
which ruins my other code. I have tried using formatIn and specifying it, but then it returns an error if no value for midnight is given.
Any help would be appreciated.
You can use datenum and datestr to convert the format to what you want. In the following example I'm assuming your timestamps are contained in a cell array of strings, but it also works if it's a char matrix:
>> timestamps = {'08/25/2014 13:14'; '08/26/2014 14:15'} %// mm/dd/yyyy HH:MM
>> result = datestr(datenum(timestamps, 'mm/dd/yyyy HH:MM'), 'dd/mm/yyyy HH:MM')
result =
25/08/2014 13:14
26/08/2014 14:15
What Luis recommended should help you to get any format that you like. However there is something important to realize here:
Excel does not 'have' the date in your format. It has the date stored as a number like 123546.123 and presents it to you in a certain way.
If you want to get the date in exactly the way that excel presents it, the trick is to avoid importing the relevant column as a date, but just import it as text instead.
How to do this depends on your import method, but it should not be very hard.

NSDateFormatter Date Format for date/time with colon in Time Zone

I am having a problem converting a date/time string to an NSDate.
The date string I need to convert is: 2002-12-24T00:00:00-07:00
My date format string is currently yyyy-MM-dd'T'HH:mm:ssZZZ
The problem is the colon in the time zone.
ZZZ returns a string like: -0700 (without the colon)
How can I convert a string WITH a colon in the time zone?
Thanks
I suggest doing some string manipulation so it is in a form that dateWithString can more easily accept - how about using stringByReplacingOccurrencesOfString one or more times to get rid of the colon?
dateWithString wants:
YYYY-MM-DD HH:MM:SS ±HHMM
you have:
yyyy-MM-dd'T'HH:mm:ssZZZ
You will probably need to use some combination of componentsSeparatedByString (to get rid of the 'T' part, unless you have a small range of values possible for T, and perhaps write yourself a small function to convert ssZZZ into +HHMM.