This might be a silly question, but I can't seem to find the answer on here or in the documentation.
I want to convert an NSString such as #"9/22/2010 3:45 PM" to an NSDate.
I know to use NSDateFormatter, but the problems are
The month could be one or two digits
Likewise, the date could be one or two digits
Hours could be one or two digits
What do I do about AM/PM?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
there is no problem in 2 digit day or 2 digit month.
This must help you.
You can parse an NSString into an NSDate using the NSDateFormatter class. See the documentation for more info:
Instances of NSDateFormatter create string representations of NSDate (and NSCalendarDate) objects, and convert textual representations of dates and times into NSDate objects.
I was having the same problem, not sure why NSDateFormatter isn't working for me (iOS5 - Xcode 4.3.2) but this worked out for me:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString = #"05-06-2012";
NSDate *date;
NSError *error = nil;
if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
NSLog(#"Date '%#' could not be parsed: %#", dateString, error);
}
[dateFormatter release];
Related
I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])
This could be repeated one but I am not able to find the correct solution.
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormat stringFromDate:receiveddate];
NSLog(#"Date: %#", dateString);
receiveddate is NSDate and its value is 2012-06-11 00:00:00 +0000
I just want to convert the above date into NSString where value should be just 2012-06-11
But when I checked it out it gives me different output that is 2012-00-11 00:00:00. It's strange. How can I get out from this problem.
You Can Use This :
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateFormat:#"yyyy-MM-dd"];
Hi I am using the same code what you have posted and it's giving me the right output what You want. So i think the problem with the format of your receiveddate. Here is tha code what i m using
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"Date: %#", dateString);
OUTPUT:Date: 2012-06-06
use this
[dateFormat setDateFormat:#"YYYY-MM-dd"];
dateFormat.dateStyle = kCFDateFormatterShortStyle;
The problem I find out is 2012-06-11 00:00:00 +0000 is not a valid NSDate.
Maybe the problem is that you are using YYYY instead of yyyy ?
Atleast I'm using it like this: [mDateFormatter setDateFormat:#"yyyy-M-dd"]; to get 2012-6-5.
I'm working on an assignment that allows the user to display events that are happening 'today'. I have parsed the XML file and stored the contents into an array. The contents of the XML file consists of a title, description, date etc. The dates are in NSString format and I want to convert them into NSDates and compare them with today's date before displaying them in a UITableView.
I'm new to obj-c and I've searched online for help on NSDate, but I couldn't find what I need. Any links, advice or help on this is really appreciated. Thanks in advance (:
suppose dateString contains the date in string format
first get date from string:-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *dateprevious = [formatter dateFromString:dateString];
Now get today date
NSDate *date=[NSDate date];
[formatter setDateFormat:#"dd"];
NSString *dateOfGame =[formatter stringFromDate:dateprevious];
NSString *todaydate =[formatter stringFromDate:date];
[formatter release];
if([todaydate isEqualToString:dateknown])
{
NSLog(#"date matched");
}
Depending on the format of the string, you can use this:
+ (id)dateWithNaturalLanguageString:(NSString *)string
To compare two dates you will find here a lot of usefull answers :)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];
You can use this one
Have a look at NSDateFormatter
It has a method called dateFromString
Like you could do the following:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:#"5/5/2011"];
I have the following date: 2011-04-29T14:54:00-04:00
When it runs through the following code to convert it to a date, the date is null for some reason:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:localDate];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-mm-dd"];
NSLog(#"%#", [formatter stringFromDate:date]);
Any help would be appreciated
SOLVED:
Ok, I figured it out. For some reason, this method doesn't work.
NSDate *date = [dateFormatter dateFromString:localDate];
This works instead. Hope it helps someone!
NSError *error = nil;
NSDate *date = nil;
[dateFormatter getObjectValue:&date forString:localDate range:nil error:&error];
Ok, I figured it out. For some reason, this method doesn't work.
NSDate *date = [dateFormatter dateFromString:localDate];
This works instead. Hope it helps someone!
NSError *error = nil;
NSDate *date = nil;
[dateFormatter getObjectValue:&date forString:localDate range:nil error:&error];
Try setting the timezone:
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
and you might need to quote the -'s, :'s, and the Z in your format string (maybe not the -'s and :'s, but I think at least the Z):
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
Other than those, that is how my date formatter is configured and it works fine.
NSDateFormatter seems to only implement the canonical formats for timezones with 'Z' as described in UTS #35. With no leniency in parsing.
TimeZoneID+/-offset with hours and minutes separated by colon, eg. 2011-04-29T14:54:00GMT-04:00
+/-offset with no separation between hours and minutes, eg. 2011-04-29T14:54:00-0400
Try changing the format of the timezone in your localDate string.
I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];