What is the difference between these two Date Formats. First one give actual time but second on give time buy adding time zone offset value.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"Date: %#",dateConverted); //
Date: 2013-12-02 12:15:43 +0000
NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter1 setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *dateConverted1 = [dateFormatter1 dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted1);
Date: 2013-12-02 06:45:43 +0000
The problem with the 2nd format is all of the needless quotes, especially around the Z. By quoting the Z this means the Z is treated as a literal character and not the timezone format specifier.
Get rid of the quotes around the Z and both will give the same result.
The second date formatter is incorrect, the 'Z' should not be single quoted, that keeps it from being considered a format character.
Also the only single quotes that are needed are around the 'T' so that is is not considered a format character but rather a literal.
See ICU User Guide: Formatting Dates and Times
Related
I get a date 2012-11-21 03:57:39-04 and I can't get an NSDate from it. I use the yyyy-MM-dd HH:mm:sszz format. I guess I'm stuck with the time zone part. I've tried different kinds of 'Z' and 'z' types but still can't get it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:sszz"]; // "2012-11-21 03:57:39-04"
The z-Codes doesn't fit, because they always requires minutes. Try a single X or x instead.
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
X 1 -08
+0530
Z The ISO8601 basic format with hours field and optional minutes field. The ISO8601 UTC indicator "Z" is used when local time offset is 0. (The same as x, plus "Z".)
Just append #"00" and you should be fine
NSString *dateStr = [#"2012-11-21 03:57:39-04" stringByAppendingString:#"00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *en_US_POSIXLocale =
[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormat setLocale:en_US_POSIXLocale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
Try this format:
[dateFormatter setDateFormat:#"yyyy-MM-dd' 'HH:mm:sszzz"];
I found the error. The format wasn't the problem because the above format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for disturbing.
answer posted by the OP Iryna Tsimokhautsava
I found the error. The format wasn't the problem because the above
format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for
disturbing.
I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.
This is the String i receive:
2014-12-22T06:49:40+0000
And this is how i m trying to format it and get it to a NSDate:
NSString *time = #"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];
Unfortunately myDate is always nil.
Am i missing somtheing or is my formatting simply wrong?
Thanks in advance
Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"
You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.
I have to format the date as below:
19-JUL-2014 10:27:16 IST
How can I do that? Should I send "IST" as string object?
I tried -
NSDate* sourceDate = [NSDate date];
NSLog(#"Date is : %#", sourceDate);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSLog(#"TimeZone is : %#", currentTimeZone);
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ;
dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss Z"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#",[dateFormatter stringFromDate:sourceDate]);
I've tried several of scenarios of timezone formatters according to the Apple's official docs and the Unicode date-formatter standards.
I inited the timezone like this:
NSTimeZone *_timezone = [NSTimeZone timeZoneWithName:#"IST"];
that presented the proper timezone to me with +0530 offset, so that was used for the instance of my NSDateFormatter.
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc]init];
[_dateFormatter setTimeZone:_timezone];
here is the list about I've experienced with different format-scpecifiers:
z = GMT+0530 IST
zz = GMT+0530 IST
zzz = GMT+0530 IST
zzzz = India Standard Time IST
zzzzz = India Standard Time IST
it seemed that none of the standard format-specifiers could provide the actual "IST" only as string, a match was the "India Standard Time IST" with format specifier zzzz and zzzzz – but you can see the "GMT+0530 IST" still contains it with the rest of the formatters.
NOTE: the other format specifiers like Z, v, V, x or X did not seem useful either.
I've read more about format specifiers, the docs says about using z:
The short specific non-location format (e.g. PDT). Where that is unavailable, falls back to the short localized GMT format.
that means to me, the actual short specific non-location format for India Standard Time is not available via NSDateFormatter directly – or for some reason is specified as "GMT+0530 IST" not as short "IST"†.
on the other hand, I'm not sure whether the long specific non-location format is accepted on your server side (aka "India Standard Time IST"), or the timezone must be marked by string "IST" only.
I'm afraid if that latest format is expected only you will need to add it manually and inelegantly, like:
[_dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSString *_date = [[_dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:#" IST"];
NOTE: I've also spotted the months' names should be capitalised as well, I'm not sure that is another expectation or the generic capitalisation of months' names (like e.g. "Jul", "Sep" etc...) is good enough for your server side – I did not take care of capitalising them in my current answer.
† I have not found any standard which to be supposed to describe the actual short format, so based on the unicode standards I would assume the "IST" should be the shortened format against the "GMT+0530 IST" – but that is based on my personal speculation only.
NSDate* sourceDate = [NSDate date];
NSLog(#"Date is : %#", sourceDate);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSLog(#"TimeZone is : %#", currentTimeZone);
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ;
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#",[dateFormatter stringFromDate:sourceDate]);
This may help you
Please try this,
NSDate *date= [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"dd-MMM-yyyy HH:mm:ss z"];
NSLog(#"%#",[dateFormatter1 stringFromDate:date]);
All,
In My project i want to convert date from MM-dd-yyyy to M/d/YY (12/31/15) but i am getting wrong format, below is the code i am using.
-(NSString*)dateConversionForWarranty:(NSString *)string
{
string i am getting as 12-31-2014
NSDateFormatter* df_utc = [[NSDateFormatter alloc] init];
[df_utc setDateFormat:#"MM-dd-yyyy"];
NSDate *date = [df_utc dateFromString:string];
When i am converting it i am getting date as this "2014-12-30 18:30:00 +0000"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"M/d/YY"];
Then when i try to convert to above format i am getting the year completely wrong "12/31/15"
NSString *formattedDate = [dateFormatter stringFromDate:date];
return formattedDate;
}
Can any one tell me where i am wrong
Don't use capital "Y" -- it produces a very unpredictable value for dates near the start/end of the year (look it up). You can use a single "M" if you wish, to suppress leading zeros in the month.
In summary:
d - day of month
M - month
y - year
h - hour -- 12 hour clock
H - hour -- 24 hour clock
m - minute
s - second
S - fraction of a second
a - AM/PM
The number of repeats corresponds to the field width (though not always 1:1). Eg "MMMM" will give you the full-length spelled-out month name, while "MMM" gives the 3-letter abbreviation and "MM" gives the 2-digit numeric month. Refer to the spec (highlighted above) for details.
i want date in this format 12/31/15 then which type of formatter
try this [dateFormatter setDateFormat:#"MM/dd/yy"];.
For more info about the Date Formatting patterns
Do it in below way..
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm"];
// or
[dateFormat setDateFormat:#"mm/dd/yyyy HH:mm"];
I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);