I am getting some date in this format "04/11/13 19:37:00" I want to convert this date to local time zone, Below is the code i am using.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"date value %#",date);
The Problem i am facing is i am using debugger when i place mouse on the "date" variable it is showing "2013-04-12 01:07:00 IST" This is the correct out put i need. But when i am printing the same "date" variable using NSLog i am getting "2013-04-11 19:37:00 +0000". How can i get the correct format can any one explay, Do i need to change the dateformat.
NSLog shows NSDate objects in a fixed format. If you want to log the date in a specific format, convert the NSDate to an NSString using an NSDateFormatter and then log the string.
Remember, logging a date is only for debugging purposes. The format in the log really doesn't matter.
If you want to convert the dateString which was in UTC to Local Timezone, you need to do it in two steps.
1.First form the date by setting the corresponding TimeZone to dateFormatter
2.Set the Local TimeZone to dateFormatter, and form dateString from date
NSString *dateString = #"04/16/13 11:12:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate* date = [dateFormatter dateFromString:dateString];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"%#",localDateString);
Related
I build app that get date string(date in french time) from a database. after i get the string i convert it to nsdate:
NSString *dateStr = "20/3/2016 21:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm zzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
self.date = [dateFormatter dateFromString:dateStr];
Into self.date i get(if i print the object) :
2016-06-10 21:00:00 +0000
But when i try to convert it to local time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSString *time = [formatter stringFromDate:cellItem.date];
I get a different time: 00:00
Any idea what can be the problem?
The default time zone for an NSDateFormatter is the time zone of the device, and as you didn't set it explicitly in the second formatter, that is what will be used.
Use the first formatter for both jobs or set it explicitly as you did in the first formatter:
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
Also a note on:
Into self.date i get (if i print the object)
If you are using NSLog() or the debugger to view an NSDate object, it will use [NSDate description] to generate the string, which will always use the GMT time zone. This constantly trips people up.
I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.
I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
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]];
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"];