I have a date in the format Tue, 29 May 2012 00:56:14 +0000 from an XML file, which is not under my control so the original format cannot be changed.
I was researching for how to create format strings for NSDateFormatter, found this page, and came up with the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE', 'dd' 'MMM' 'YYYY' 'HH':'mm':'ss' 'Z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *parsedDate = [dateFormatter dateFromString:date];
However, after parsing the date above, Tue, 29 May 2012 00:56:14 +0000, with this format string I get 2011-12-27 00:56:14 +0000. So the time is correct, but the date is all jumbled.
Could someone with some more knowledge of NSDateFormatter please explain why this isn't working? To my knowledge the format string seems correct, but apparently it is not. Thanks!!
Your year format string should be lower case, 'yyyy'
Related
I'm parsing a large number of internet dates. First I try a formatter with en_US_POSIX locale, then with en_GB. The code looks more or less like this:
{
NSDate *date = [dateString dateWithDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z (zzz)" localeIdentifier:#"en_US_POSIX"];
if (date) return date;
date = [dateString dateWithDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z (zzz)" localeIdentifier:#"en_GB"];
return date;
}
- (NSDate*) dateWithDateFormat:(NSString*)dateFormat localeIdentifier:(NSString*)localeIdentifier
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
formatter.dateFormat = dateFormat;
[formatter dateFromString:self];
}
However, date strings with the following time zones fail to parse:
Mon, 16 Jul 2012 12:08:17 +0100 (GMTUK)
Thu, 6 Sep 2012 13:00:06 +0900 (KST)
Wed, 3 Nov 2010 10:12:15 +0100 (Hora est�ndar romance)
Wed, 14 Sep 2011 14:37:35 +0100 (IST)
Wed, 2 May 2012 09:41:06 +0200 (MEST)
Sun, 31 Oct 2010 12:53:06 +0800 (SGT)
Thu, 19 Jan 2012 08:34:44 -0300 (UYT)
What am I doing wrong?
Should I pre-process the strings to remove the time zone parenthesis in these cases only?
NSDate can store a point in time without timezone information. It's up to your software to know whether a specific NSDate instance stores the point in time in UTC or in the local time zone. In most cases, you want to use UTC dates.
Because of that, it's important to handle time zone differences when parsing the dates. But it's not possible to remember the time zone the dates was originally in (at least not with an NSDate instance only).
So I would recommend that you cut off the time zone in parenthesis and just parse the numerical time zone offset before it. That way, you can convert all strings into an NSDate instance in UTC and you shouldn't have any problems parsing the strings.
And shouldn't the date fromat be (i.e. uppercase Z for a numeric time zone offset)?
#"EEE, dd MMM yyyy HH:mm:ss ZZZ"
I am using core data in my project. Database is sqlite and there is column for storing "birthdate" having datatype "timestamp".
Now, I want to insert some records in it and I am having dates in human readable format like "1809-06-17".
How to convert this date to timestamp so that, I get this date when fetched from database.
I tried some conversions using python script, but I got different results.
Is there any easy way to do this? Please help me.
You could store the date as TEXT and dont have to worry about converting to and from timestamp.
To convert timestamp to NSDate use:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
NSLog(#"%#", date);
String to NSDate to timestamp:
NSDate *date = [NSDate dateWithString:stringWithDate];
NSTimeInterval timeStamp = [date timeIntervalSince1970];
SQLite doesnt have a datatype for Date, it just stores it in a INT (Unix Time), REAL(Julian day numbers) or TEXT(ISO8601 strings) depending on what you choose.
So you could just store the date in the format you already have ("1809-06-17") as a TEXT column or you store as a timestamp in an INT column using the methods above to convert both ways.
SQLite Date and Time functions: SQLite date and time
SQLite DATATYPES: SQLite datatypes doc
SQLite Oficial Site and Documentation: SQLite Home
Hope you can solve your problem
Finally I got the solution I needed.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate * past = [df dateFromString:#"1971-04-06"];
NSTimeInterval oldTime = [past timeIntervalSinceDate:[df dateFromString:#"2001-01-01"]];
NSString * unixTime = [[NSString alloc] initWithFormat:#"%0.0f", oldTime];
NSLog(#"unixTime = %#",unixTime);
I am parsing a JSON file which contains a timestamp. I want to convert this timestamp into an NSDate.
My code :
NSString *depart=[alternates valueForKey:#"startTime"];
NSTimeInterval intervaldep=[depart doubleValue];
NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:intervaldep];
But I am getting wrong date, e.g.: 44135-04-01 03:20:00 +0000 for time stamp 1330606668000.
If any one has any idea please help.
Thank you.
Your timestamp contains milliseconds, just divide your timestamp by 1000 (i.e. trim the last three zeros) and you should be fine.
Try to add the NSTimeZone to your NSDate, by default the dates are in GMT0
You can check the syntax here:
Convert UTC NSDate to local Timezone Objective-C
I have a NSDate variable "aDate" that represents a date, for example, sunday, August 28, 2011. I have this date on a NSDate variable. This can be any date, but the problem appears when the day is a sunday.
I want to obtain the three letter string representing the day of week. In this case, SUN.
Then I have this code:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEE"];
[theDateFormatter setLocale:currentLocale]; // I have tried to comment this out... no change
NSString *dayOfWeek = [theDateFormatter stringFromDate:aDate];
the result I have is MON. Monday?
The insanity just goes wilder if I add this code:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:myDate];
int weekday = [comps weekday];
then weekday comes as 2 (MONDAY ??????) WTF? Confirming the problem.
I am on a locale that in theory monday is the first day of week. So, independently of my timezone any date just have one day of week. If I want to calculate what day of week was January 1, 1978, I shouldn't need to define any timezone.
How do I do that on iPhone?
thanks.
NSData is referenced to a base date "the first instant of 1 January 2001, GMT", it is simply the number of seconds since that base date.
Dates by definition are location sensitive, as I write this my friend in the UK is already in tomorrow.
Day of week is calendar sensitive.
Thus the timezone and calendar need to be specified. In my case they seem to be correct by default but are best specified explicitly.
So, set the timezone:
- (void)setTimeZone:(NSTimeZone *)tz
What's your time zone? Chances are you are one day off because your time zone is sufficiently different from GST that the time you are representing is the next or previous day.
I have found it necessary to specify the date as being in the local time zone to avoid this.
Hope you can help. I am importing the current GMT time for an iPhone App. This is being retrieved via a JSON web service.
I believe I have the correct formatter string however I am getting a different date (time is still correct) when I try to format the date I've retrieved. The JSON date is formatted like this: Sun, 15 May 2011 20:35:31 +0000
In the example below strGMT is the date in the format I've just mentioned.
This is the code I'm using to get retrieved date into my code:
NSLog(#"Current GMT: %#", strGMT);
NSDateFormatter *gmtFormatter=[[NSDateFormatter alloc] init];
[gmtFormatter setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss VVVV"];
//THIS IS NOT REFORMATTING CORRECTLY HERE
NSDate *gmtDateTime=[gmtFormatter dateFromString:strGMT];
NSLog(#"Current Formatted GMT Date: %#", gmtDateTime);
The log is showing the following:
Current GMT: Sun, 15 May 2011 20:35:31 +0000
Current Formatted GMT Date: 2010-12-26 20:35:31 +0000
Have I not got the formatting string correct? Any ideas why it's gone from 15 May 2011 (today) to 26th December 2010?
Kind regards
Paul
The correct format string is #"EEE, dd MMMM yyyy HH:mm:ss VVVV"