I'm sorry if this has been asked before but I did a lot of checking and I can't seem to find an answer. :(
I have a string "2013-05-7 05:53:15 +0000" and I want to convert it to a NSDate so I can compare it with the current date and time. I can't figure out how to do this. :( I successfully converted it but it changes the look of the string and I want it to be exactly 2013-05-7 05:53:15 +0000 as that is the same format I have the other NSDate item.
Any and all help is greatly appreciated.
I am confused. ;) new to objective C. old VB / older C programmer from the 80s trying to figure this stuff out. Thank you for your help. :)
My code looks like this:
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
// [df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
df1.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *titemdate1 = [df1 dateFromString: itemdate];
what i have is two dates. one that is the last date the app was run and the other is the date of the item i'm pulling down using JSON. I want to compare the two dates (which are in string format as stated above). when I do a NSLog of the strings they look perfect but when i convert to NSDate to compare i have problems. any ideas?
Try with using below code..
-(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}
First of all you should know that if you convert the dateString to date and do NSLog of date, it returns the GMT time, which will be different from your system time in most cases. So if you are getting the string in correct format, then the date formatter is probably working fine. To check this, just convert your formatted date to string and print the output.
NSLog(#"formatted date: %#",[df1 stringFromDate:titemdate1]);
If that doesnt change anything and the output is still wrong, try changing the format of your formatter.
[df1 setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
Related
I'm trying to format two NSString to an NSDate with this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/yyyy"];
NSString *dateString = #"5/1916";
NSString *dateString2 = #"6/1916";
NSDate *date = [dateFormatter dateFromString:dateString];
NSDate *date2 = [dateFormatter dateFromString:dateString2];
Guess what:
date is nil and date2 works fine (isn't nil).
Any idea how this come?
Edit:
For those who think it's about the string, the next screenshot has the samen problem.
your code works perfectly fine with following result
Printing description of dateString:
05/1916
Printing description of date:
1916-04-30 18:06:40 +0000
But issue is that NSDate is something which must have day, month , year Just revamp your code to feed dateformatter a correct date.
in most languages, 5/1916 wouldn't be accepted, but 05/1916 is, since the format is MM/yyyy not M/yyyy. the length of string is mandatory.
hope that helps,
eiran
I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.
This is the String i receive:
2014-12-22T06:49:40+0000
And this is how i m trying to format it and get it to a NSDate:
NSString *time = #"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];
Unfortunately myDate is always nil.
Am i missing somtheing or is my formatting simply wrong?
Thanks in advance
Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"
You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.
Here's the code in question:
// configure created_at date for display
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"formattedDate=%#",dateFormatter.dateFormat);
NSLog(#"created_at=%#",notice.created_at);
NSString *createdDateStr = [dateFormatter stringFromDate:notice.created_at];
NSLog(#"createdDateStr=%#",createdDateStr);
self.labelCreated.text=createdDateStr;
And here's what's coming out per NSLog:
2014-06-29 12:06:48.945 [3589:60b] formattedDate=MMM d, y
2014-06-29 12:06:48.947 [3589:60b] created_at=2013-02-22T08:49:52Z
2014-06-29 12:06:48.948 [3589:60b] createdDateStr=(null)
This looks a lot more complicated that it needs to be, but I was following examples from the Mac Developers Library (iOS 7.x, Xcode 5.x) Any guidance would be greatly appreciated.
Your created_at date format is different. So first you need to set the dateformat accordingly as created_at dateformat like that below:-
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
Please consider, if you are converting date format from string, you should write the format exactly like in your string with spaces etc. Other ways date format will return null
Not sure what I'm missing here. The following code returns a valid NSDate but with an incorrect time.
In fact it doesn't matter what the dateString's time returned is always incorrect so I don't think it is a timezone issue but a formatter issue.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss+HH:mm"];
[inputFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *dateString = #"2013-07-26T19:45:00+01:00";
NSDate *theDate = [inputFormatter dateFromString:dateString];
// returns an NSDate of 2013-07-26 01:00 BST or 2013-07-26 00:00 in NSLog
Anyone spot where I'm going wrong? Thanks, M.
Well there is a small mistake in your date formate, you are not handeling the date offset (timezone). NSDateFormatter will not pick the latest found time as the time not the timezone.
In your code yyyy-MM-dd'T'HH:mm:ss+HH:mm you are parsing the time twice, you should use yyyy-MM-dd'T'HH:mm:ssZZZZ. the ZZZZspecifies the time zone.
Be aware that this code will only work on iOS6 and higher, if you need support for iOS 5 you should remove the : from the timezone.
I am trying to get a string from NSDate object type and display it in a label.
Here is the code I am using,
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *datey = [dateFormatter dateFromString:selectedDate];
NSString *labelData = [dateFormatter stringFromDate:datey];
dateLabel.Text = labelData;
where selectedDate is a String containing date in yyyy-mm-dd format.
The label is not showing up the date. But if I try giving it a string like #"someblah", it is displaying. Where am I going wrong??
Help appreciated. Thanks
Your line dateLabel.Text should be dateLabel.text.
So you start with a date string, convert it to a date, and then convert it back to a string using the same formatter. Isn't it easier to use:
dateLabel.text = selectedDate;
In all likelihood you are trying to create an NSDate using a date string that is inconsistent with the format. For example, if the format is #"yyyy-MM-dd" but your selectedDate differs from that format then the formatter won't return a NSDate. You can avoid this by setting the DateFormat to be correct for the selectedDate and then, once parsed, change the DateFormat for the desired output. Or use two NSDateFormatter instances.
Try specifying a specific format instead of NSDateFormatLongStyle, for example
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
I found this answer in 2 seconds using google.. (Yes thats a hint)