ios date time in am pm date formatter issue - iphone

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.

Related

nsdateformatter not working with japanese calendar

I'm seeing a problem with NSDateFormatter on iOS when Settings/General/International/Calendar is set to either Japanese or Buddhist on both the simulator and a real device. The year is not parsed correctly
DateFormatter
static NSDateFormatter *formatter = nil; // cache this the first time through
if (formatter == nil) {
formatter = [NSDateFormatter new];
formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]; // Fix for QC79748 - Michael Marceau
formatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
formatter.calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease];
}
NSLog(#"CorrectDate%#",[serviceHeaders safeObjectForKey:#"Date"] );
NSLog(#"Formatted date:%#",[formatter dateFromString:[serviceHeaders safeObjectForKey:#"Date"]]);
Output
Correct - Mon, 27 Aug 2012 16:33:14 GMT
Formatted date:0024-08-27 16:33:14 +0000
This is working just as it should. I took your code and added it to my project, then set the Simulator to use the Buddhist calendar.
NSLog(#"date=%#", [NSDate date]);
2012-08-27 18:42:10.201 Searcher[43537:f803] date=2555-08-27 22:42:10 +0000
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]; // Fix for QC79748 - Michael Marceau
formatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]];
Then the output:
NSLog(#"CorrectDate%#", #"Mon, 27 Aug 2012 16:33:14 GMT" );
2012-08-27 18:42:10.203 Searcher[43537:f803] CorrectDateMon, 27 Aug 2012 16:33:14 GMT
NSLog(#"Formatted date:%#",[formatter dateFromString:#"Mon, 27 Aug 2012 16:33:14 GMT"]);
2012-08-27 18:42:10.206 Searcher[43537:f803] Formatted date:2555-08-27 16:33:14 +0000
Analysis:
Its all working perfectly. In the Buddhist calendar, the year is 2555 now. When you provide a Gregorian date, and ask the formatter to use the Gregorian calendar, it reads it in properly, then converts it to the Buddhist date, and when you print that out, the date is 2555 again. Just what you would expect.
EDIT: Just to press the point, the NSDate is always the same, what changes is the representation of it. So I set the calendar to Buddhist again, and used your formatter to get the current time in Gregorian time:
NSLog(#"date=%#", [NSDate date]);
NSLog(#"Date using the Gregorian calendar: %#", [formatter stringFromDate:[NSDate date]]);
outputs
2012-08-28 07:13:10.658 Searcher[69194:f803] date=2555-08-28 11:13:10 +0000
2012-08-28 07:13:10.660 Searcher[69194:f803] Date using the Gregorian calendar: Tue, 28 Aug 2012 07:13:10 EDT
PS: your code sets locale twice.

Iphone: NSString to NSDate

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".

How to convert date string into format "17 Nov 2010"

I have the date that is in format 2010-11-17.This is of the form NSString.
I want to convert this NSString date into format 17 Nov 2010 and display in a label
This date is just not the current date but may even be the older dates.
So I cant use the [NSDate date] instance to get the date.
I tried using NSDateFormatter with the method dateFromString.
But it gives me a null value.
Here is the code snippet
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);
What should be done?
Please Help and suggest.
Thanks.
Try this
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"yyyy-MM-dd"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"d MMM yyyy"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"RESULT %#",result);
I got the result.Correct me if this approach is wrong.
Here are some formats for who desire to know!!
12:16:45 PM on January 01, 2000 --> hh:mm:ss a 'on' MMMM dd, yyyy
Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy--
09/12/2018 --> MM/dd/yyyy--
09-12-2018 14:11 --> MM-dd-yyyy HH:mm--
Sep 12, 2:11 PM --> MMM d, h:mm a--
September 2018 --> MMMM yyyy--
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
12:16:45 PM --> hh:mm:ss a
AM or PM --> a
NSDateFormatter uses date format patterns from UNICODE.
So for your 17 Nov 2010 date you need something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d MMM yyyy"];
NSString *result = [formatter stringForObjectValue:date];
Where date variable contains the date you'd like to format.
UPDATE: Try changing your code to this:
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);

GMT format change

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?

How do I convert from NSString to NSDate?

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.