how to convert from NSString to NSDate
?
it must be in yyyy-MM-dd HH:mm:ss this format when it converts from NSString to NSDate format
Use NSDateFormatter's dateFromString:
NSString *dateStr = #"20100212";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
For further ref. Check out:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
Made an NSString extension for that.
// Simple as this.
date = dateString.dateValue;
Thanks to NSDataDetector, it recognizes a whole lot of format.
// Tested in GMT+1 (Hungary).
#"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000>
#"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000>
#"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000>
#"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000>
#"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000>
#"20140116" dateValue is <2014-01-16 11:00:00 +0000>
#"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000>
#"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000>
#"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000>
#"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000>
#"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000>
#"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000>
#"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000>
Part of eppz!kit, grab the category NSString+EPPZKit.h from GitHub.
Related
i am trying to change date string get from data base to date(mm.dd.yy) and time(am pm).
this way i am getting date time from sqlite:
rs=[db executeQuery:#"select *,strftime('%m.%d.%Y %H:%M',update_date) AS DATETIME from sales_order where deleted=0 and status=1 and customer_id=? order by order_id desc",custId];
i get date time string :
NSString *dateTimeString = [rs stringForColumn:#"DATETIME"];//from nslog 02.25.2014 14:41
then i initiate date formatter and get date:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[self getCurrentLocale]];
[dateFormatter setDateFormat:#"mm dd YY hh:mm a"];//it is nil,i am stuck here also tried with mm.dd.YY hh:mm a
NSDate *date = [dateFormatter dateFromString:dateTimeString];
Any suggestion
thanks
I have tried following code:-
NSString *dateStr = #"02.25.2014 14:41";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"MM.dd.yyyy HH:mm"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"date = %#",date);
And I got Following output log:-
date = 2014-02-25 09:11:00 +0000
Let me know, It will help or not.
Why don't use NSDataDetector.
Whether you're not sure (or don't care) about the date format contained in the string, use NSDataDetector for parsing date.
NSString *dateString = #"Wed, 03 Jul 2013 02:16:02 -0700";
__block NSDate *detectedDate;
//Detect.
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
[detector enumerateMatchesInString:dateString
options:kNilOptions
range:NSMakeRange(0, [dateString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{ detectedDate = result.date; }];
Made an NSString extension for that.
// Simple as this.
date = dateString.dateValue;
Thanks to NSDataDetector, it recognizes a whole lot of format.
'2014-01-16' dateValue is <2014-01-16 11:00:00 +0000>
'2014.01.16' dateValue is <2014-01-16 11:00:00 +0000>
'2014/01/16' dateValue is <2014-01-16 11:00:00 +0000>
'2014 Jan 16' dateValue is <2014-01-16 11:00:00 +0000>
'2014 Jan 16th' dateValue is <2014-01-16 11:00:00 +0000>
'20140116' dateValue is <2014-01-16 11:00:00 +0000>
'01-16-2014' dateValue is <2014-01-16 11:00:00 +0000>
'01.16.2014' dateValue is <2014-01-16 11:00:00 +0000>
'01/16/2014' dateValue is <2014-01-16 11:00:00 +0000>
'16 January 2014' dateValue is <2014-01-16 11:00:00 +0000>
'01-16-2014 17:05:05' dateValue is <2014-01-16 16:05:05 +0000>
'01-16-2014 T 17:05:05 UTC' dateValue is <2014-01-16 17:05:05 +0000>
'17:05, 1 January 2014 (UTC)' dateValue is <2014-01-01 16:05:00 +0000>
Part of eppz!kit, grab the category NSString+EPPZKit.h from GitHub.
I am definitely not new to NSDateFormatter or NSDate but this has completely stumped me. How in the world am I getting fire date wrong?
NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
[firedateFormat setDateFormat:#"hh:mm a EEE dd MMM, YYYY"];
[firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *fireDate1 = [firedateFormat dateFromString:#"5:30 AM Sat 27 Oct, 2012"];
NSLog(#"fireDate1: %# ...", fireDate1);
OUTPUT:
fireDate1: 2011-12-31 10:30:00 +0000 ...
It was a smaller error, switch the YYYY to yyyy
NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
[firedateFormat setDateFormat:#"hh:mm a EEE dd MMM, yyyy"];
[firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *fireDate1 = [firedateFormat dateFromString:#"5:30 AM Sat 27 Oct, 2012"];
NSLog(#"fireDate1: %# ...", fireDate1);
Output: fireDate1: 2012-10-27 10:30:00 +0000 ... (Date in GMT)
A great reference for all Date Formatters http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/
How to get this format of date from NSString;
Wed, 22 Jun 2011 12:36:00 +0100 to Wed, 22 Jun 2011.
Thanks
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
The minimalistic version is E, d MMM y or to specify 2 digit days and 4 digit years E, dd MMM yyyy. The Date Formatter uses the Unicode Technical Standard #35.
Hi Im trying to convert an NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:lastModified];
But when I do a
NSLog(#" date: %# ", dateFromString);
I get date: (null)
The lastModified string has date in string format as "Tue, 15 Feb 2011 13:30:42 GMT"
What am I doing wrong?
your input (lastModified) does not match the specified date format.
a properly formed string representation of the time specified by lastModified for the date format is:
"2011-02-15 13:30:42 GMT", but you're passing "Tue, 15 Feb 2011 13:30:42 GMT".
I get the date from URL as GMT Format as follows.
Thu, 13 Nov 2010 05:15:01 GMT
I want to display that date as ordinary format.
I used the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH':'mm':'ss"];
NSDate *date = [dateFormatter dateFromString:#"Thu, 13 Nov 2010 05:15:01"];
NSString *strDate = [dateFormatter stringFromDate:date]; //[NSDate date]]; // Ordinary Format
NSLog(#"Date is ........ %#", strDate);
[dateFormatter release];
This above code display the following output.
Date is ........ Sat, 13 Nov 2010 05:15:01
But when I put the date as follow the date "Thu, 13 Nov 2010 05:15:01 GMT" in NSDate then the out put is display as NULL.
How I will edit this
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH':'mm':'ss"];
to this date "Thu, 13 Nov 2010 05:15:01 GMT"
Can't you simply remove the GMT substring from your string and call the dateFromString: on the resulting string?