I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35
Related
I created a NSDateFormatter but sometimes it creates an NSDate out of a given string sometimes it doesn't on the same device (Simulator).
This is my code:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];
For Example for the string
"Fri, 09 Jan 2015 15:08:01 GMT" it returns nil but for "Fri, 12 Dec 2014 09:15:52 GMT" it returns the right NSDate.
I tried to add parenthesis to the dateformat and I tried to set locale to "en_US_POSIX" and i tried a little z instead of a big one but neither of it worked.
What am I doing wrong? Thanks for your help.
NSDateFormatter follows the Unicode standard for date and time patterns. Use 'H' for the hour on a 24-hour clock:
So in your code, the correct way should be:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];
I have a problem with NSDateFormatter parsing a string coming from the web.
I parse dates as string and transform them to NSDate. The problem is in choosing the correct format string.
Parsed dates have following "format":
Feb 04, 2014 8:00 AM ET
but I haven't find the correct format to transform them into an NSDate
I've tried with: EEE dd, yyyy hh:mm a but it is not working.
Code is simple:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd, yyyy hh:mm a"];
NSDate *articleDate = [formatter dateFromString:articleDateString];
Any idea?
Try below code to convert your NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm a 'ET'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *articleDate = [dateFormatter dateFromString:#"Feb 04, 2014 8:00 AM ET"];
NSLog(#"-->%#",articleDate);
Using this page: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change your "EEE" to "MMM" and add a "z" at the end to catch your timezone.
"EEE" is day of week, for example, "Tue", not month.
Trying to convert the following string to an NSDate - I thought my DateFormatter was set correctly, but its not working:
NSString *dateToCheckString = #"Friday, Dec. 6, 2013 at 7:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"eeee, MMM. dd, yyyy at hh a"];
NSDate *dateToCheckNSDate = [[NSDate alloc] init];
dateToCheckNSDate = [dateFormatter dateFromString:dateToCheckString];
// Log it out to see the result:
NSLog(#"Conversion yielded: '%#'", [dateToCheckNSDate description]);
The output I get is (null)
Any ideas?
No it's not correct. dd is a padded day (06 in your example) so that should be just d. Also "7:00" does not match hh (which is padded hours). You are looking for h:mm
I am getting date string form mail server like this. Thu, 31 Dec 2009 14:32:15 +0580.
I want to convert this date string to date.
Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *inputstring=#"Mon, 3 sep 2012 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
[dateFormatter setLenient:YES];
NSLocale *enUS = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:enUS];
NSDate *result = [dateFormatter dateFromString:inputstring];
NSLog(#"test==%#",result);
}
I am getting output null.
Excepted Output : 2012-09-03 03:02:39 +0000
First problem: You are not trying to read the "Tue" on the string. (Add "EEE, " to the front of your format)
Second and bigger problem: +0580 is not a valid time zone. There was a PHP bug a few years ago that mistakenly returned IST (+0530) as +0580. +0580 makes no sense. It means 5 hours and 80 minutes. So you can do one of two things: Either replace +0580 with +0530 before you process it or set the date formatter time zone to be IST and remove the +0580 from the string.
I see you accepted another answer, but that answer "works" because it fails to parse the final part and ignores the time zone. I ran it and got 2013-07-09 08:32:38 +0000 (Which is not the same as 2013-07-09 08:32:39 +0580)
Removing the + in the accepted answer's format causes the formatter to parse correctly, but you will get null because the timezone is invalid. Changing the time zone to +0530 gives the expected result of 2013-07-09 03:02:39 +0000
Replace
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss ZZZ"];
with
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzzz"];
Just copy and paste the below code in your project and run, see the result
NSString *dateString = #"Tue, 9 Jul 2013 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MM yyyy hh:mm:ss +zzzz"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"test==%#",dateFromString);
#Raviteja Kammila use following code for date format like 2013-07-09 03:02:39 +0000
NSString *dateString = #"Tue, 9 Jul 2013 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MM yyyy HH:mm:ss +zzzz"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"final date : %#",date);
I parse a date from an xml file and store it in a string variable like this :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
When i try to print this variable on console i get :
the object value is:Fri, 01 Jun 2012 15:52:00 GMT
I am trying to change the format of the above date , to be stored again in a string variable but like this : dd/mm/yy.
I tried this code :
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
But when i print the dateText variable on console i get :
the object value is:(null)
What am i doing wrong and the date is never stored in the variable?
Thanks!
Try this,(setDateFormat line is changed).
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"]; // changed line in your code
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"]; // changed line in your code
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
For Calender year use: 'yyyy' and for month use: 'MM'
Probably you parsing format is wrong. Check, if NSDate* date is filled correctly.
Perhaps you need another d, because you get the day with a leading zero.
Try: EEE, dd MMM YYYY HH:mm:ss Z.
Your date format for the first dateformatter is wrong. It should be
[dateFormatter setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss Z"];
Hope this helps.