NSDate give me the error date, Anybody know why? - ios

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE MMM DD HH:mm:ss Z yyyy";
NSLog(#"Original Time:%#", _created_at);
NSDate *newCreatedDate = [dateFormatter dateFromString:_created_at];
NSString *newString = [dateFormatter stringFromDate:newCreatedDate];
NSLog(#"Conversion Time:%#", newString);
This is the result:
Original Time:Sun Oct 25 18:37:03 +0800 2015
Conversion Time:Sun Jan 25 18:37:03 +0800 2015

You should be using dd instead of DD.
dateFormatter.dateFormat = #"EEE MMM dd HH:mm:ss Z yyyy";

Related

Date formate to convert NSString (Wed Jul 29 14:46:11 +0000 2015) to NSDate in ios

I have date in NSString format as "Wed Jul 29 14:46:11 +0000 2015". I have tried all possible ways of converting this string into NSDate. But couldn't find the right format for type of string.
Can someone help out or give suggestion to convert this into NSDate.
Below is the code:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MM dd HH:mm:ss Z YYYY";
//[format setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"];
[format setLocale:locale];
NSDate *date = [format dateFromString:#"Wed Aug 19 17:36:46 +0000 2015"];
NSLog(#"converted date is %#",date);*
But its returning wrong date as converted date is 2014-12-24 17:36:46 +0000
Thanks.
You need to set your NSDateFormatter's dateFormat to EEEE MM dd HH:mm:ss Z YYYY.
You can use the following code for converting the specified string to date.
Objective C:
NSString *string = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MMM dd HH:mm:ss Z yyyy";
NSDate *date = [format dateFromString:string];
Swift:
var dateStr = "Wed Jul 29 14:46:11 +0000 2015"
var formatter = NSDateFormatter();
formatter.dateFormat = "EEEE MMM dd HH:mm:ss Z yyyy"
var date = formatter.dateFromString(dateStr)
Are you sure that:
you have the right format for NSDateFormatter when calling dateFromString()?
you have the right locale?
you have looked up any errors returned?
Please post the code and your failure message/ results.
See documentation and related SO post
You can follow this. It helps you.
NSString *dateString = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE MMMM dd yyyy HH:mm:ss Z yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

iOS create date from string

How can I make this format in iOS: "Mon Jan 26 07:57:33 +0000 2015"?
I have tried this
NSString *createdAt=#"Mon Jan 26 07:57:33 +0000 2015";
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"ddd MMM dd HH:mm:ss zzzz yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSDate *date=[formatter dateFromString:createdAt];
but this returns me nil always
// Convert string to date
//Fri, 23 Jan 2015 14:24:24 IST
NSString *beginString = [currentFields objectForKey:#"pubDate"];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Kiev"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
This is my example. Your date formatter should be the same as your original string date.

Covert string with timezone to Date

Here is the date string
NSString * dateString = #"Mon Sep 29 14:40:00 2014 PET";
How to convert it into NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Above code doesn't work.
what is the correct date format for above case?
the problem is your timezone definition (PET), because that is not on list of the recognisable timezones (please don't ask why not); this timezone is mostly known as UTC-5 in Apple's system, therefore:
NSString *_dateString = #"Mon Sep 29 14:40:00 2014 PET";
_dateString = [_dateString stringByReplacingOccurrencesOfString:#"PET" withString:#"UTC-5"];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[_dateFormatter setCalendar:[NSCalendar currentCalendar]];
[_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(#"date : %#", _date);
and the console says:
date : 2014-09-29 19:40:00 +0000
which is the correct representation of the original date in GMT+0 timezone.

NSDate in ios conversion

i wanto convert the string 2013-03-19 13:41:57 +0000 to this format Wed Feb 27 2013 00:00:00 GMT+0530 (India Standard Time) in iOS .please help me any help will be highly appreciated.
i tried some thing like this:
NSString * clippedString = #"2013-03-19 13:41:57 +0000 ";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE MMM d yyyy HH:mm:ss Z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:clippedString];
I accepted the challenge :)
NSString *string=#"2013-03-19 13:41:57 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate *date=[dateFormatter dateFromString:string];
[dateFormatter setDateFormat:#"EEE MMM dd yyyy HH:mm:ss zzz zzzz"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);

Is it possible to parse "Fri, 24 Feb 2012 20:00:00 GMT" with using NSDateFormatter?

I'm trying to parse
Fri, 24 Feb 2012 20:00:00 GMT
Is it possible to do this with using NSDateFormatter?
Try the following:
NSString *dateString = #"Fri, 24 Feb 2012 20:00:00 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
Depending on how you will specify the time zone, you may have to change the line:
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
with
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss Z";
if you will use GMT-02:00 for example, but keep it if you will use PDT.
Sure you should use a mask..
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] ];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateString];
It could work.

Resources