NSString to NSDate returns wrong date - objective-c

I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.

This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];

As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright

In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)

As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];

Related

Objective C - Convert DateTime to Local DateTime

I am pretty new to Objective C and it has been horrible experience to get the current device datetime in systemTimeZone. This is what I have:
NSDate *now = [NSDate date];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[DateFormatter setDateFormat:#"MM-dd-yyyy HH:mm"];
NSString *currentDateTime = [DateFormatter stringFromDate:now];
NSDate *curDate = [DateFormatter dateFromString:currentDateTime];
Line 5 NSString has the correct local datetime in currentDateTime string variable. But Line 6 again switches back to UTC DateTime. I do not understand why it would switch back to UTC even though the DateFormatter has the systemTimezone set. Can you please help me find out what is that I am missing?
Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];
or You can also try this one:
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
// you can also use NSDateFormatter dateFromString to go the opposite way
Table of formatting string parameters:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
If performance is a priority, you may want to consider using strftime
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/strftime.3.html

NSDate returning nil for the NSDateFormatter set correctly

I am getting the date in NSString format as mentioned below.
Input:datestring = 30/04/2013 04:49 PM
I am applying the format for NSDateformatter as mentioned below to convert this NSString to NSDate .
Format:dateFormat= dd/MM/yyyy hh:mm a
I still get the date conversion to Nil. Please help as I am not understanding this issue.Here is my code snippet to explain what i am doing in the utility function.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
Try This....
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:#"30/04/2013 04:49 PM"];
NSLog(#"%#",date);

Convert date time to NSDate from NSString

Time zone always made trouble for me. This time I have to convert date-time , that I have in NSSTring to NSDate.
I am doing something like this.
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSLog(#"%#",aDate);
but I am having the output like this
14-11-2012 11:09:00 +0000
Also, no AM/PM is setting :-(
What I want is 14-11-2012 4:09:00 PM +0500 i.e same Date-Time that I have in string.
Thanks in anticipation.
the output of your converting is giving the GMT time
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:aDate];
NSString* ts_local_string = [df_local stringFromDate:aDate];
Here Is the Change need to be taken.
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a "];
Thanks
The conversion looks correct, but the default output is in UTC time (+0000) and with a 24h clock which is why you don't get the AM/PM distinction (“01:00 PM” would be 13:00). You also need to format the output if you want a specific representation ([formatter stringFromDate:aDate])…
For the UTC issue you need to select the time zone of your choice, by giving that choice in place of [NSTimeZone systemTimeZone]. Try [NSTimeZone localTimeZone] for starters.

NSString to NSDate conversion issue

I have Date is 2012-09-25 09:33:10 +0000 , using
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *d = [dateFormatter dateFromString:sDate];
// sDate = #"2012-September-21"
But i get nil , instead of a formatter date. What can be the issue ?
Since your sDate = #"2012-September-21", it does not have time (hour minutes and seconds) in your string sDate. So you should set your format like this
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
You don't include time into string. You also need to fix the locale:
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[dateFormatter dateFromString:#"2012-September-21"];
From your question it is not clear that what is input, but i have tested with below given input it is working
NSString *dateString = #"2012-09-25 09:33:10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:dateString];

How can I parse this string to NSDate with NSDateFormatter?

I have a date as a string in the format:
2010-12-31 20:21:00 +0200
What I'd like to do is parse this using NSDateFormatter to an NSDate object but I'm having difficulty matching the format properly.
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone: NSTimeZone.localTimeZone];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss'Z'"];
[dateFormatter dateFromString: #"2010-12-31 20:21:00 +0200"] // returns nil ;
Can anybody help me find the correct format? thanks
This code works:
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone:NSTimeZone.localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:#"2010-12-31 20:21:00 +0200"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
And I just want you to know, kind sir, that my eyes bleed when I see this NSDateFormatter.alloc.init
NSDateFormatter* format = [[NSDateFormatter alloc]init];
[format setDateFormat:#"MM/dd/YYYY"];
NSString * currentDate = [format stringFromDate:[NSDate date]];
NSDate *date = [format dateFromString:currentDate];