Convert UTC to CST in NSDateFormatter - ios

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.

Related

Converting calendar date to the correct date

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

NSDateFormatter stringFromDate: returning local time instead of UTC / GMT

inside my iOS App I have an NSDate Object that should be converted to a NSString showing date and time in UTC / GMT:
NSDateFormatter * dateTimeFormatter = [[NSDateFormatter alloc] init];
[dateTimeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// OR not specify the TimeZone at all...
NSString *result = [dateTimeFormatter stringFromDate:date];
When I set a breakpoint and examine date it is correctly displayed as __NSDate * 2015-08-25 14:03:57 UTC. However the resulting string is 2015-08-25 16:03:57, which is the same date in my local TimeZone (CEST).
No matter which solution I tried to get a string in UTC /GMT format 2015-08-25 14:03:57, I always get the local version 2015-08-25 16:03:57.
All sources I found state, that this should work. Where is my mistake?
This code should works:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[formatter setTimeZone:timeZone];
NSDate *now = [NSDate date];
NSString *dateAsString = [formatter stringFromDate:now];
The only strange thing I see in your code it's that you use dateFormatter instead of dateTimeFormatter when set the NSTimeZone.

Getting nil from dateFromString when converting string to date

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.

NSDate time issue

I am getting an value of 2015-04-22 10:43:57 from server but getting problem when converting into NSDate. I'd like to convert the same into NSDate into same format given in NSString. Here is my code
// getting from server
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [dateFormat dateFromString:dateString];
If I will get dateString with timezone then will it be resolved.
Your code looks correct.
I imagine you are having a problem with timezones while formatting. You culd addnit to your NSDateFormatter to specify if input date is from an specific timeZone
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
Please use below code
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
dateFormat.timeZone=localTimeZone;
NSDate *aDate = [dateFormat dateFromString:dateString];
OutPut: 2015-04-22 10:43:57 +0000
Hope it helps you..!
Your code is correct. You just try adding this line to your code.
//This will set time zone of your device.You can change systemTimeZone to localTimeZone based on your need.
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

Conversion of NSString to NSDate failing

I have the following piece of code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
I am getting the NSLog statement as Parsed Date.. 2011-12-25 00:00:00 +0000. I would appreciate any help in resolving this. I just want to convert that NSString to its NSDate equivalent. Tried using NSTimeZone as well, but no effect. What am I missing here?
You need to use lower case year indication, yyyy instead of YYYY:
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
If you want to get the timezone right you need to set the timezone of the NSDateFormatter. By default it uses the timezone of your device. For the time at GMT use the following.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
You are using an incorrect format string. See the documentation. Basically, capital YYYY is "week of year year". Try to use lowercase yyyy instead, i.e.
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];

Resources