NSDate Formatter time zone issue - ios

I have a NSString like this."10/28/2014 9:20 PM PDT".I have to convert exactly this to NSDate.But on conversion,the time zone is converted to IST.This is what i tried.
departTime=#"10/28/2014 9:20 PM PDT"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a zzz"];
NSDate *departTimedate = [dateFormatter dateFromString:departTime];
This is what i am getting on converting to NSDate: "2014-10-29 04:40:00 +0000"
Can anyone help me on this.

Related

There is differnce in date while I convert string to date

I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDateFormatter not giving expected results

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM YYYY"];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);
I am trying to format the date 14 Oct 2013, to get an NSDate object out of it. But for some reason, it is not formatted as expected.
Here's the date object that I get after logging it on console,
formatted date 2012-12-22 18:30:00 +0000
Anything wrong I am doing here?
You should use lowercase yyyy because uppercase one represent a week based calendar year and you should specify timezone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);

IST time zone not getting parsed

I have a string Tue Feb 23 10:12:48 IST 2014, I want to get the NSdate object form this. I am using below code snippet
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:buildTime ];
Every time I am getting nil value to date variable. If I am changing IST to GMT in build time then NSDate is perfectly coming. Can any one correct me where I am doing mistake in this.
Use small 'zzz' instead of 'ZZZ',
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSLocale *indianEnglishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *date = [dateFormatter dateFromString:buildTime];
NSLog(#"date %#",date);
NSLog(#"string %#",[dateFormatter stringFromDate:date]);

NSString to NSDate with setDateFormat

I have a problem with NSDateFormatter parsing a string coming from the web.
I parse dates as string and transform them to NSDate. The problem is in choosing the correct format string.
Parsed dates have following "format":
Feb 04, 2014 8:00 AM ET
but I haven't find the correct format to transform them into an NSDate
I've tried with: EEE dd, yyyy hh:mm a but it is not working.
Code is simple:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd, yyyy hh:mm a"];
NSDate *articleDate = [formatter dateFromString:articleDateString];
Any idea?
Try below code to convert your NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm a 'ET'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *articleDate = [dateFormatter dateFromString:#"Feb 04, 2014 8:00 AM ET"];
NSLog(#"-->%#",articleDate);
Using this page: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change your "EEE" to "MMM" and add a "z" at the end to catch your timezone.
"EEE" is day of week, for example, "Tue", not month.

iphone How to convert today date and time to GMTFormat(10 digits)

I have locale date and time I am using the following code
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(#"current time:%#",currentTime);
Here i got localTime
Now i am converting local time to GMT Time
I am using the following code
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"hh:mm:ss MM dd yyyy "];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter1 setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
Now my problem is i want to convert this GMT date into 10digit format like 1362468453
For that purpose i am using
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
How to get GMT Time in tendigits format
Use timeIntervalSince1970 function for your date to get epoch time.
NSLog(#"Epoch Time : %f", [dateFromString timeIntervalSince1970]);
Check this NSDate Class Reference

Resources