The input string is '20100908041312'
the format is year,month,day,Hours,minutes,seconds,time zone
and I have ben trying to convert it to an NSDate with this: #"yyyyMMddHHmmsszz"
But NSDate is nil, anyone see what I'm doing wrong?
-(NSDate*)convertPDFDateStringAsDate:(NSString*) _string{
//20100908041312
//year month day Hours minutes seconds and time zone
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMddHHmmsszz"];
NSDate *date = [dateFormat dateFromString:_string];
//date is nil...
[dateFormat release];
return date;
}
EDIT: its the time zone breaking it
EDIT2: #"yyyyMMddHHmmssTZD" stops it returning nil, but dosnt pick the time zone correctly
EDIT3: This was the code I Used in the end...i found that the format changes from PDF to PDF so the code deals with the variations that i Found, in some cases this wont extract the Time Zone Properly.
-(NSDate*)convertPDFDateStringAsDate:(NSString*) _string{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString * formatCheck = [_string substringToIndex:2];
if(![formatCheck isEqualToString:#"D:"])
{
NSLog(#"ERROR: Date String wasnt in expected format");
return nil;//return [NSDate date];
}
NSString * extract = [_string substringFromIndex:2];
//NSLog(#"DATESTRING:'%#'",extract);NSLog(#"DATELENGTH:%i",[extract length]);
if([extract length]>14)
{
[dateFormat setDateFormat:#"yyyyMMddHHmmssTZD"];
}
else
{
[dateFormat setDateFormat:#"yyyyMMddHHmmss"];
}
NSDate * date = [dateFormat dateFromString:extract];
[dateFormat release];
return date ;
}
It should follow the unicode standard for the setDateFormat: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
You should be able to set the timezone this way:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyyMMddHHmmss"];
//Optionally for time zone conversations
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
Alex
I recognize the posting is over a year old, but no time is too late for a better answer.
Since you specify the input string is an Adobe PDF date string, then the format should conform to the PDF specification, which per the spec is: YYYYMMDDHHmmSSOHH'mm (omitting the prefix D:).
Note your input string is 14 characters long and your NSDate format is 16 characters long, so the timezone isn't in your input string as stated.
Nonetheless, the real answer to your question is to use the Quartz 2D CGPDFString function:
CFDateRef CGPDFStringCopyDate (CGPDFStringRef string
);
The function returns a CFDateRef which has a toll-free bridge to NSDate, so you can pass the date read from a PDF to this function and easily get back an NSDate by casting.
"12" isn't a valid time zone for any of the Unicode date format patterns, so NSDateFormatter returns nothing. http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
You may have to use all but the last two digits, then set the time zone on your new NSDate object by translating Adobe's two-digit number to an appropriate time zone.
Related
I want to convert string to date. Its not working in following situation.
I am using settings application to set region. In settings, go to general, go to international, go to region format, go to spanish and then set Argentina.
After setting these parameter, I try to convert string to date. Its not converting string to date. Please help.
EDIT : This is my code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
NSDate *_date = [formatter dateFromString:strDate];
This document explains what you're experiencing
In short, format your date with the american locale, if you wish to display the date to a user, convert the date to a string.
For example:
NSString *strDate = #"05/29/2012 6:15PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *_date = [formatter dateFromString:strDate];
And to show the date to a user:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *newDate = [formatter2 stringFromDate:_date];
Date strings entering the application should typically be converted to a standard format, should you ever want to display a date to the user, you would need to format the date back to a string - think of them as seperate steps:
Get the string to a sane date format
Get the date to the users locale with the desired formatting
When you are converting a String to Date, the String must be in Spanish(Argentina) format for the NSDateFormatter to understand.
in your example, the date you passed is "05/29/2012 6:15PM"
However, in Spanish(Argentina), the date string should be "05/29/2012 6:15p.m."
If you are getting this date dynamically, best is to remove the AM/PM component in the date and try getting the Date String in 24-Hour format
I have to convert this NSString: "12/13/1980" to a NSDate object.
I use a code like this:
NSString *birthday = #"12/13/1980"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
But the result is this: "1980-12-12 23:00:00 +0000"
Why? I'd like the same format and the same date.
Thank you very much.
The [dateFormat dateFromString:dateString] method produces an NSDate object which represents a single point in time.
When you NSLog a date object, it is printing a system representation of the NSDate object. I'm assuming your locale is GMT+1 .. so the NSLog prints 12/12/1980 23:00.
If you want to print the date object back, use your formatter to do [dateFormat stringFromDate:date];
It's because a NSDate object has always to have a time and a timezone, so if you don't specify that in your the string you're trying to convert IOS will use your local timezone and then guess the time, wich in this case will be 23:00 in UTC 0
The format is dependent on the localisation, there's also the option to set whether the date/time is short, medium or long format - I believe short is what you're looking for:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
How can I compare the dates only, not the time. I am using
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *tempDate = #"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(#"%#",dateString);
It logs this: 2012-02-01 18:30:00 +0000
NSDate *now = [NSDate date];//Current Date
NSLog(#"%#",now);
It logs this: 2011-04-04 14:49:45 +0000
I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.
Suppose I have to strings
date1 = 3-2-2011;
date2 = 4-5-2020;
I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.
Thanks!
Simplest way is to compare date by converting it into string.
Sample Code is as shown below:
//Current Date
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
//Other Date say date2 is of type NSDate again
NSString *date2String = [formatter stringFromDate:date2];
//Comparison of Two dates by its conversion into string as below
if([date2String isEqualToString:dateString])
{
//Your logic if dates are Equal
}
else if(![date2String isEqualToString:dateString])
{
//Your Logic if dates are Different
}
EDIT:
Checkout this link.
Comparing dates
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html
Hope This Helps You. :)
Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.
If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
I have spent way too much time (over an hour) on what I though would be a two minute task.
On the iPhone:
NSString * dateString = #"2010-09-11T00:00:00+01:00";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ssTZD"];
NSDate *date = [formatter dateFromString:dateString];
RESULT: date == nil
What am I missing!! (Besides my deadline)
Regards,
Ken
TZD isn't a defined formatter per the unicode spec. The document you've linked to elsewhere was a suggestion someone made to W3C, for discussion only. The unicode standard followed by Apple is a finished standard, from a different body.
The closest thing to what you want would be ZZZ (ie, #"YYYY-MM-dd'T'HH:mm:ssZZZ"), but that doesn't have a colon in the middle. So you'd need to use the string:
2010-09-11T00:00:00+0100
Rather than the one you currently have that ends in +01:00.
E.g. the following:
NSString * dateString = #"2010-09-11T00:00:00+0100";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"%#", date);
Logs a valid date object, of 2010-09-10 23:00:00 GMT.
Tip: try using your formatter to convert from an NSDate object to a string, then see what you get. It's often easier to debug in that direction than the other.
Have you read this?
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
That TZD at the end of your format string looks a bit dodgy.
I solved this issue using this code.
Writing an iPhone app in Objective-C, I have a date in string form (in UTC format, with a Z on the end to denote zero UTC offset, or zulu time), which I need to parse into an NSDate object.
A bit of code:
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString* str = #"2009-08-11T06:00:00.000Z";
NSDate* date = [df dateFromString:str];
Running this through the debugger, date ends up nil! I'm assuming it has something to do with my date format string.How can I fix it to correctly parse the date string?
A thought would be to make the Z in the date format literal, a la setting the date format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.
That would work, except when the Z is parsed as a literal, the date loses offset information, and so is ambiguous, and therefore interpreted to be local time.
For example, if the string to parse was 2009-08-11T06:00:00.000Z (6:00 zulu time) it would be interpreted as 6:00 local time, and an incorrect offset would then be applied. It would then be parsed as 2009-08-11T06:00:00.000-600 (12:00 zulu time) with the offset depending on the user's offset.
Thanks!
I've had this problem also, I'm not sure if it's a API bug within Apple's code, or my lack of understanding, but I've worked around it by using hour offsets in my date strings.
If you change the code in your example to:
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString* str = #"2009-08-11T06:00:00.000-0700"; // NOTE -0700 is the only change
NSDate* date = [df dateFromString:str];
It will now parse the date string. Of course the -0700 hours is my offset, you'd have to change it to yours. Hope this helps.
Most answers suggest you to treat 'Z' as a literal character. Do not do this!
The Z actually means that the date is offset by 0 to UTC (+0000).
This is according to the time zone format ISO8601:
ISO 8601 time zone format: A constant, specific offset from UTC, which always has the same format except UTC itself ("Z").
"-08:00"
"Z"
What you want to do is use the following format for your NSDateFormatter:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 2013-11-18T23:00:00.324Z
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
return formatter;
By repeating the Z five times, you tell the formatter to use ISO8601 when parsing the string.
Bonus:
Use one to three Zs for RFC 822 GMT format.
Use four Zs for
localized GMT format.
For more information check this document.
I think you need to put single quotes around the Z in the format string, because the Z actually means something to the formatter and you want it to represent a literal character instead.
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
There's no need to manipulate the string. Simply set the time zone on the NSDateFormatter (and the locale while you're at it):
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setLocale:[NSLocale systemLocale]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
And parse date strings as needed:
NSDate * value = [formatter dateFromString:#"2012-03-01T23:08:25.000Z"];
NSLog(#"%#", value); // prints 2012-03-01 23:08:25 +0000
You can use this method to get date from UTC.
+ (NSDate*)getDateFromUTCDateTimeString:(NSString*)dateString {
NSDateFormatter *isoDateFormatter = [[NSDateFormatter alloc] init];
[isoDateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
[isoDateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [isoDateFormatter dateFromString:dateString];
return [dateFormatter dateFromString:[userFormatter stringFromDate:date]];
}
this may help you.. the "Z" is a literal for the time zone code. try using "o" (the letter, not zero). The literal "o" means UTC offset. I ran into this a while back, I hope this helped you.
-(NSDate*)dateFromZulu:(NSString*)str {
if (str == nil) {
NSLog(#"Error getting date");
return [NSDate date];
}
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *ret = [f dateFromString:[str stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
[f release];
if (ret == nil) {
ret = [NSDate date];
NSLog(#"Error formatting date (%#)",str);
}
return ret;
}