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

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.

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')

Converting non standard date using datenum

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.

How do I parse "YYYY-MM-DD" with joda time

I'm trying to use joda-time to parse a date string of the form YYYY-MM-DD. I have test code like this:
DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("YYYY-MM-DD");
DateTime dateTime = dateDecoder.parseDateTime("2005-07-30");
System.out.println(dateTime);
Which outputs:
2005-01-30T00:00:00.000Z
As you can see, the DateTime object produced is 30 Jan 2005, instead of 30 July 2005.
Appreciate any help. I just assumed this would work because it's one of the date formats listed here.
The confusion is with what the ISO format actually is. YYYY-MM-DD is not the ISO format, the actual resulting date is.
So 2005-07-30 is in ISO-8601 format, and the spec uses YYYY-MM-DD to describe the format. There is no connection between the use of YYYY-MM-DD as a pattern in the spec and any piece of code. The only constraint the spec places is that the result consists of a 4 digit year folowed by a dash followed by a 2 digit month followed by a dash followed by a two digit day-of-month.
As such, the spec could have used $year4-$month2-$day2, which would equally well define the output format.
You will need to search and replace any input pattern to convert "Y" to "y" and "D" to "d".
I've also added some enhanced documentation of formatting.
You're answer is in the docs: http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
The string format should be something like: "yyyy-MM-dd".
The date format described in the w3 document and JodaTime's DateTimeFormat are different.
More specifically, in DateTimeFormat, the pattern DD is for Day in year, so the value for DD of 30 is the 30th day in the year, ie. January 30th. As the formatter is reading your date String, it sets the month to 07. When it reads the day of year, it will overwrite that with 01 for January.
You need to use the pattern strings expected by DateTimeFormat, not the ones expected by the w3 dat and time formats. In this case, that would be
DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("yyyy-MM-dd");

Convert a formatted date string to a date value in XPath

How can I convert a date contained in a string into a date value with XPath?
I got the string by formatting a date value with fn:format-date, and now I want the date value from the formatted string.
Thank you,
You can use EXSLT's date:date(string). It is implemented in most XSLT processors but also as a pure XSLT function.
Documentation: http://www.exslt.org/date/functions/date/index.html.