Is it possible to use an NSDateFormatter to convert between Unix time i.e. 1334688300703 to an NSDate?
I'm using Magical Record to import records and trying to use the dateFormat user attribute to convert.
If not I'll just do the conversion manually.
You don't need a date formatter for that.
Use
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:unix_time];
or
unix_time = [theDate timeIntervalSince1970];
respectively.
The NSDate instance method
- (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds
would appear to be your best bet.
Related
This is a really common question, but mine might be unique since I have long decimal places for the seconds.
NSString *timestamp = #"2015-11-06 15:27:34.0000000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss zzz"];
NSDate *capturedStartDate = [dateFormatter dateFromString: timestamp];
My capturedStartDate is null.
For the date format, I've tried replacing zzz with Z, a, and removing it completely. I've also tried with and without the 'T'. Does someone know the correct format to retrieve this date from the string?
Your date string doesn't have a time zone in it, so you should remove that Z. Also, you have fractions of a second, too. And there's no T in the date string. So you want
yyyy-MM-dd HH:mm:ss.SSSSSS
This does beg the question as to what time zone that string represents. If it is UTC, you'll want to set the time zone of the formatter, accordingly.
Likewise, you might want to be careful about users with non-Gregorian calendars. See Apple Technical Q&A 1480 regarding setting the locale to en_US_POSIX.
I'm having problems using a custom date formatter with NSDateFormatter to convert a string into a date. Here's a short example that creates a string from today's date but fails to convert this back to an NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"Mdyyyy"];
NSString *example = [dateFormatter stringFromDate:now]; // e.g., 10292013
NSDate *reverse = [dateFormatter dateFromString:example]; // nil?
So basically it seems that NSDateFormatter is creating a date string that it itself can't turn back into a NSDate using the same format that created the string.
Using MMddyyyy as the date string works, although I can't see from the documentation (which conveniently only goes up to iOS 6.0) why it would matter:
month M 1..2 09 Month - Use one or two for the numerical month, ....
...
day d 1..2 1 Date - Day of the month
The reason why I'm trying to use Mdyyyy instead of MMddyyyy is because it's closer to what NSDateFormatterShortStyle returns for my current NSLocale (M/d/yy).
Perhaps someone might have some insight here as two what I'm doing wrong, or if I'm wrong in my understanding of how this should work. (I know there are a lot of questions here regarding NSDateFormatter, but I didn't find one that fits my problem.)
Mdyyyy is ambiguous as a string ->date mapping. One cannot tell if "1112013" is Jan 11 or November 1. Hence NSDateFormatter will not allow it for string ->date.
What is the easiest way in ios app to compare datetime string in sqlite3.
I store the datetime string in following way.
NSDateFormatter* df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString* dtString=[df stringFromDate:[NSDate date]];
then I'm inserting dtString into the table. So after i want to retrieve all rows for example only for last two days.
sqlite3 doesn't contain a DATETIME type so you need to think of ways of encoding it.
If you want to use the date column in a WHERE clause then you could store the time_t returned from gmtime(time(NULL)) (i.e. the time now, since UNIX epoch) and then use some simple clause to get those rows:
... WHERE date >= ?
And binding that ? to gmtime(time(NULL)) - 60**60*24*2
EDIT: If you want to stay completely in Objective-C, then the same date value can be determined from an NSDate object using:
(time_t)[date timeIntervalSince1970]
When I am printing the date
//getting the current date and time
self.date = [NSDate date];
NSLog(#"%#",date);
The date which I am getting is correct, but there is a delay in time by 6 hrs. My system time is correct.
try this
NSLocale* currentLoc = [NSLocale currentLocale];
NSLog(#"%#",[[NSDate date] descriptionWithLocale:currentLoc]);
Make use of NSDateFormatter
NSDate *today = [NSDate date];
//Create the dateformatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the required date format
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//Get the string date
NSString *dateString = [dateFormatter stringFromDate:today];
//Display on the console
NSLog(dateString);
Logging an NSDate in the debugger is somewhat misleading as it gives you a calendar day and time for a particular time zone - UTC / GMT. However, NSDate has no inherent time zone or any inherent relationship to how humans perceive and think about dates at all. Instead, it is a timestamp. Classes like NSDateComponents, NSTimeZone, NSDateFormatter, and so on all exist to provide human context and formatting.
So what you see is the timestamp formatted to that particular format and UTC time zone, which is how NSDate will always appear when printed in the debugger or the console. If you were to calculate the time zone offset between UTC and your own time zone, you'd find that the date represents the time stamp you gave it, and not one however many hours off.
you can set current time zone for customizing your date format.
This link can help:
https://stackoverflow.com/a/7213629/456471
The default date string representation is probably formatting the date as UTC, rather than your local time zone (the exact format that it will use is not defined, and may change from release to release, so you shouldn't rely on it). You should use the NSDateFormatter class if you need to format a date in a particular format (or with a particular time zone, including the local time zone); see the Data Formatting Guide and the NSDateFormatter Class Reference for more information.
I'm having a problem with NSDate instance.
For the date I'm receiving 2012-08-16 00:00:00 +0000.
But I need to remove from the 00:00:00 +0000.
Is there any way to do that?
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *eventDate = [formatter dateFromString:currentDate]; // current Tile
NSLog(#"date %#",eventDate);
Use
NSLog(#"date %#",[formatter stringFromDate:eventDate]);
A NSDate is just a single point in time. It does not know how it's value should be formatted, that's what NSDateFormatter is for.
NSLog(#"date %#",eventDate); prints the default string representation of the date.
A date formatter converts strings into dates and dates into strings. An NSDate represents an point in time, regardless how you created it. In your code you seem to be thinking that the NSDate will "remember" that the formatter it came from didn't specify a time of day; it won't. When you call dateFromString: it will simply pick 00:00:00 (midnight) as the time.
If you need to work with calendar dates regardless of the time, you can either:
Use NSDate objects and ignore the time component. You will need to be wary of time zones, since midnight on August 15th in one time zone can be 11pm on August 14th in another.
Use a different data structure to store year, month, and day. NSDateComponents is a good candidate, or you could create your own.