Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
and my date string is #"2015-08-22 13:00:00"
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:#"2015-08-22 13:00:00"];
return date;
The last line returns nil.
You need to change:
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
With:
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Since hh refers to the 12 hour clock and HH refers to the 24 hour clock.
hh represents hours in 12-hour-mode and 13 is out of range.
Replace hh with HH for 24-hour-mode
I had exactly the same problem last week and I solved it with this code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateFromString.doubleValue];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:date];
Here you have a string named dateFromString because this is the object type for date which I recieve from server. This string is converted in double value and the date is calculated with time interval.
I suppose you need this date to be shown in a label or text view or another UI element like this, and just because of that I converted the date into NSString to be easy to use it. This is what I have in stringDate.
Let me know if I have to help you more.
smaller case letters for 12 hour clock and upper case for 24-hr. Change hh with HH.
Related
I am getting the following string to be used, but it is from UTC-0, and I need to convert it to CST.
serverdate = # "2017-07-31 02:18:50";
I added the following code, but it returns nil
NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm"];
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *time =[dfLocal stringFromDate:serverdate];
NSLog(#"%#", time);
I even tried the following options, no luck.. still it returns nil.
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CDT"]];
and tried
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
First you need to handle the seconds in your time:
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Next set the timezone to UTC and convert your string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *serverUTC = [dfLocal dateFromString:serverdate];
Now change the time zone and convert back to a string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
NSString *time =[dfLocal stringFromDate:serverUTC];
Use America/Chicago as that will handle DST correctly.
HTH
Try this answer:
NSString *strInputDateString = #"2017-07-31 02:18:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"dd-MM-YYYY hh:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);
A few thoughts:
You actually need to do two separate things here; first, you need to parse the UTC date string you've received from the server into a date, and second, you need to display that date as a string in CST.
Your dateFormat does indeed need to include the :ss at the end, as Steven mentioned.
If there's not a specific reason that you need CST, but rather the problem is something like this being an internal app for a company that happens to be located in the Central time zone area, then I'd suggest using [NSTimeZone defaultTimeZone] instead. This allows your app to keep on Just Working™ if somebody suddenly needs to use it somewhere else, and will also automatically handle things like daylight savings.
With all that said, here's some code that should do what you want:
NSString *serverdate = #"2017-07-31 02:18:50";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [formatter dateFromString:serverdate];
// [NSTimeZone timeZoneWithName:#"CST"] should work too
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *time = [formatter stringFromDate:date];
NSLog(#"%#", time);
I think, you should change your dateFormat from yyyy-MM-dd HH:mm to yyyy-MM-dd HH:mm:ss.
In Following Code I am trying to convert NSString to NSDate and once again converting to another Date format and sending it to server.
Following code worked perfectly fine when my Device Time Format is 12 hr. with dateformat to [dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"]; it gives me correct Date and Time.
But As soon as I changed device time Format to 24 hr. and change the datefomatter to [dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"]; it gives wrong Date and Time (Specifically time.) I had shown log values for code.
I had checked lot of stackoverflow questions regarding NSDateFormatter and Conversion of NSString to NSDate. But I am not able to find out where I'm making mistake and why it gives me wrong time when my Device time format is 24hr. Please help me to resolve this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
Following log showing Date and Time When My Device Time format is 12hr. And it is Correct one.
From DateTime 2017-07-04T13:46:00
Time When My Device Time format is 24hr. And it is wrong (specifically time).
From DateTime 2017-07-04T12:46:00
Please try setting locale & timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: #"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);
I am aware that this question is asked too many times but none matched my requirement.
I have "8-8-2015 12:00:00 AM" in NSString named strEventTimeBegin. How do I convert this NSString to NSDate? Here's what I tried but returns null.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *startEventDate = [dateFormatter dateFromString:strEventTimeBegin];
From memory, but this should work:
[dateFormatter setDateFormat:#"d-M-yyyy hh:mm:ss a"];
Just to explain, in your example;
The day and the year are backwards.
You're using 2 digit days and
months (It's looking for 08-08-2015).
You're missing the period
(AM/PM) identifier.
I Know it may be got answered a lot here but i think i have a different case
I'm getting the string date in the following format
2014-04-14 16:04:07 +0000
And using the following code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [dateFormat dateFromString:st];
But startDate always give me nil
You should handle zone too. Correct variant:
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Also consider to use nice lib for date formatting ISO8601DateFormatter. It will handle your format automatically.
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
If that format does not match the format of the date string exactly, the result of dateFromString: is nil.
It doesn't so it is.