NSDateFormatter timezone (Etc/GMT) not getting parsed for dateFromString - iphone

I am getting nil for trying to convert this 2011-07-11 13:28:59 Etc/GMT into a NSDate by the following code
NSString *dateString=#"2011-07-11 13:28:59 Etc/GMT";
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];
NSDate *purchaseDate = [dateformatter dateFromString:dateString];
This date is coming from apple server itself for in-app purchase validation. I've gone about lot of threads with timezone. but none of them show how to convert this Etc/GMT
i have tried z zz zzz zzzz Z ZZ ZZZ ZZZZ
what might be the issue here.

The correct dateFormat is yyyy-MM-dd HH:mm:ss VV which is what you should use to solve your problem.

// Swift Version
let DateString = "2016-01-21 00:29:09 Etc/GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
var dateObject = dateFormatter.date(from: DateString)

NSString *dateString=#"2011-07-11 13:28:59 Etc/GMT";
dateString = [dateString stringByReplacingOccurrencesOfString:#"Etc/GMT" withString:#"GMT"];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
// Or stuff....>>
NSDate *date = [dateformatter dateFromString: dateString];
NSLog(#"You may Need This =========%#",date);
dateformatter = [[[NSDateFormatter alloc] init] autorelease];
[dateformatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *convertedString = [dateformatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
/// may help =====>> http://unicode.org/reports/tr35/

Related

How to get the time in am/pm format iphone NSDateFormatter?

Am working in an iPhone app with using the NSDateFormatter. Am getting the time from the webservice like "00:00:00", "16:00:00","15:30:00" and so on. I need to convert these time to
if the time is "00:00:00" to "12 AM", if the time is "16:00:00" to "4 PM", if the time is "15:30:00" to "3.30 PM". I have used the below code to this but when i have pass "16:00:00" the date returns null.
NSString *dats1 = #"16:00:00";
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:#"hh:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(#"date1 : %#", date1); **// Here returning (null)**
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
When am passing "00:00:00" it is returning "12 AM" correctly. Can anyone please help me to solve this issue?
If your time format is in 24hrs please use "HH:mm:ss" format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"
NSString *dats1 = #"16:00:00";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:#"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(#"date1 : %#", date1); **// Here returning (null)**
[dateFormatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
Thanks.
You can use the .ShortStyle which will adapt to the current locale:
let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())
Which returns 17:12 or 5:12 PM depending on the current device locale.
Why are you again allocating the NSDateFormatter ?
you Just need to format it to NSDate and then again to NSString,
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dats1];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"%#",formattedDate);
Try HH:mm:ss (notice the capitals in the hour format)
Hope someone would be helpful who would like to get it in Swift
Swift 5.1+
let dateFormatter = DateFormatter()
//if you want 12AM,5PM then use "hh mm a"
//if you want 00AM,17PM then use "HH mm a"
dateFormatter.dateFormat = "hh mm a"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
This Below code always keep in 12hour format:
NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
df.locale = 12hourFomrat;
This always keep in 24 hour format
NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
df.locale = 24hourFormat
your date format should be:
[dateFormatter3 setDateFormat:#"HH:mm:ss"];
check the Date Format Pattern Doku
Swift 5:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())

How to make date conversion?

I am getting date in string formate ad "2012-04-11T07:51:10+0000" & I want to convert it to NSDate as "11-04-2012". I dont understand how to use "setDateFormat" for this.
This is what I am doing--
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'zzzz"];
Please help.
And whatever NSDate I will get I again want to convert to string ie. only 11-04-2012.
I have searched on ggl. But no satisfactory result :(
please help.
Here is how to parse the string to generate a date:
NSString *dateString = #"2012-04-11T07:51:10+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyy-MM-dd'T'HH:mm:ssZ";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
Now you can generate a new string representation of the date as follows:
formatter.dateFormat = #"dd-MM-yyyy";
NSString *newDateString = [formatter stringFromDate:date];
NSLog(#"New string: %#", newDateString);
// 11-04-2012
The problem with the dateFormat you were using is that you don't have to escape all the symbols such as 'MM' and that zzzz is not the correct symbol to parse the timezone of type +0000.
Have a look at this documentation for more information.
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100"; // change value base on your requirement.
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"]; // Change format base on dareStr.
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"]; // Change format base on your output requirement
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];

Date does not appear in UILabel

What is wrong with this code?
NSString *strTest = #"Sun Apr 12 23:29:24 +0000 2009";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy"];
NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
self.createdAt.text = [dateFormatter stringFromDate:createdAtFormatted];
self.createdAt is a UILabel and it stays empty. Why?
When trying to use dateWithNaturalLanguageString, I get:
Your date formatter date format does not match the date format from the strTest string.
NSString *strTest = #"Sun Apr 12 23:29:24 +0000 2009";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]; // so the formatter recognizes english names for months and days
[dateFormatter setLocale:loc];
[loc release]; // remove if using ARC
[dateFormatter setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
NSLog(#"got %#", createdAtFormatted);
The formatter specifiers can be found here.
You are giving MM/dd/yy pattern to dateFormatter but your date string is not fit for that thus dateFormatter returning a nil date.

NSString to NSDate keeps getting null

I know this been asked for so many times but I always end up getting null in my NSDate. I have a string like this "January 16, 2012 21:44:56" I want to convert it to "January 16, 2012 09:44:56 PM". I want to add a PM in the converted date and convert the 24 hour time format to 12 hour time format. Here's my code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:ii:ss a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
As Ali3n correctly pointed out, you should first set the format of dateString to the formatter to get a valid date object. Next you should set the formatter's format to the desired one and continue. Do the following:
NSString *dateString = #"January 16, 2012 21:44:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:mm:ss a"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
#"MMMM dd, YYYY HH:ii:ss a" this format should match with the date the ypu are passing to the date formatter ..
There is an error in your format string an also you need to tell the formatter the Locale in which your date string is presented.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss a"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release], dateFormatter = nil;
Setting the Local is very important since you have an name of a date in your input. You will need to tell the NSDateFormatter is wich language the name will be. In the example given it is in english. I've you run you code without setting the local on a device where the language is not set to english it wil fail to parse the date.
Try to escape literals in the format string.
[dateFormatter setDateFormat:#"MMMM dd',' YYYY HH:ii:ss a"];
As for your requirements you have to change the dateFormatter.Check this link for more.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSLog(#"%#",dateString);

Unable to format UTC date with NSDateFormatter

I have the following date:
2011-10-20T01:10:50Z
I would like it to be formatted to
"M/d/yy 'at' h:mma"
Here is my code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-dd-MM'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:#"created_at"]];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
[dateFormatter release];
NSLog(#"%#", finalTime);
Unfortunately the finalTime is NULL here.
You have dd-MM backwards. You have 10 for dd and 20 MM. There is no month 20.
2011-10-20T01:10:50Z
#"yyyy-dd-MM'T'HH:mm:ss'Z'"
Because of that, the newTime was null and the finalTime was null.
Here's with the fix. This:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:#"2011-10-20T01:10:50Z"];
NSLog(#"original time: %#", newTime);
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);
[dateFormatter release];
Outputs:
2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM
This technical note helped me resolve the same issue: https://developer.apple.com/library/ios/qa/qa1480/_index.html
In a nutshell, you need to set 3 things on NSDateFormatter:
Locale
TimeZone
DateFormat
code sample:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2014-10-23T01:10:50.000Z"];
I was using NSDateFormatter to successfully parse UTC strings to NSDate by only setting time zone and date format. When it suddenly stopped working I found this tech note and added setLocal: which resolved the issue for me.
Because Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let dateHopefully = dateFormatter.dateFromString("2016-07-29T17:15:02Z")
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
dateFormatter.dateFormat = "M/d/yy 'at' h:mma"
let lastTime = dateFormatter.stringFromDate(dateHopefully!)
print("Here is the hopeful date \(dateHopefully) and here is the lastTime \(lastTime)")