I am using Core-Datato store date-time object.
Eg. 2015-07-28T07:16:52+0000 this is date in ISO format in GMT timezone.
But when I save this date in database
NSString* dateString=#"2015-07-28T07:16:52+0000";
NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter dateFromString:dateString];
the resulting date which I see in database is 2015-07-28 12:46:52
which is in IST according to my device's timezone
I tried to set timezone as well in dateFormatter but again the same response
NSString* dateString=#"2015-07-28T07:16:52+0000";
NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateTimeformatter dateFromString:dateString];
Even when I save the [NSDate date] directly in database , it saves the converted date according to device's timezone.
Why Core-data is not taking the timezone of NSDateFormatter into account?
Can anyone tell me how can I save the date in GMT in database irrespective of my device's timezone?
I think the behavior you're seeing is the 3rd party software converting it to your timezone for display. Timezones don't really matter except for display purposes (or if you need to convert between them for calculations, maybe). And, NSDates on their own don't really correspond to any particular timezone, though internally they're represented as though they were GMT. The doc says:
The sole primitive method of NSDate, timeIntervalSinceReferenceDate,
provides the basis for all the other methods in the NSDate interface.
This method returns a time value relative to an absolute reference
dateāthe first instant of 1 January 2001, GMT.
If you look at how the value is actually stored (which you can do with the sqlite3 command-line tool), you can see Core Data is storing it as the number of seconds since 1/1/01; there's no timezone involved. I have an app which does a ton of date manipulation and stores dates in a Core Data store. It looks something like:
zach$ sqlite3 TaskLog.sqlite
sqlite> select ZSTARTTIME from ZTASK;
459924925.598104
459925327.3355
459925356.467429
...
I managed to solve problem I was facing by doing some research and using zpasternack's answer .I mentioned some findings for anyone who may face such problems related to timezone.
Findings:
The date which is saved in database (originally in any timezone) will be saved in 'GMT/UTC' timezone.
The date which is seen in local timezone in database directly is because of third party tool which is used to see the data in database.
When date is taken out of database , then also it is in 'GMT/UTC' timezone.
To see the date in specific timezone , the NSDateFormatter is used with the specific timezone specified.
The most important is NSDate object is always irrespective of any timezone and is always in 'GMT/UTC'
Related
I have a Date column in my Sqlite database. When I put some NSDate value in it, and open the Sqlite file through graphical tools. I get some value like 530963469.705571. The corresponding date is around today or yesterday.
It's clearly not Unix epoch datestamp value. What exactly does this value mean?
I need to change this value in order to debug my app.
From the documentation
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
Since sqlite does not support NSDate directly, the date is persisted as the underlying value. 530963469.705571 represents the number of seconds between 00:00:00 UTC on 1 January 2001 and the time when you created the NSDate
You can use the NSDate initialiser init(timeIntervalSinceReferenceDate:) to create an NSDate from the time interval value.
I am currently creating an app where I ask the user to input a time (through datePicker) and send the user a notification every day on that time.However, I noticed that datePicker only has an NSDate object, and was wondering if there was a Time object counterpart.Also, would this Time object be a good way of storing the time, or should I convert the hour and minutes to Integers for storage?
Thanks in advance!
NSDate also contains Time information within it. You can store the exact date and time using only NSDate.
NSDate is a generic representation independent of any time zone. According to the apple docs:
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001)
NSDate is basically the number of seconds from the reference date mentioned above.
And yes NSDate is the best way for storing time and date information in your app.
Whenever you want to display the time, use NSDateFormatter to format the date and time into any format you desire and for any Time Zone you require.
Swift 3
The NSDate class has been renamed to Date in Swift 3
This question already has answers here:
Get NSDate from NSDate adjusted with timezone
(2 answers)
Closed 6 years ago.
I need to get current time in NSDate, not swift. I tried to do this like on screenshot, but the final nsdate is in wrong timezone.
An NSDate does not have a time zone. It records an instant in time on planet Earth. It is DISPLAYED in a particular time zone.
An NSDateFormatter will convert between NSDate objects and date strings (in either direction).
If you install a time zone into your date formatter then it will convert dates to/from strings using that time zone. If you don't specify a time zone it will use the user's current time zone.
If you try to display an NSDate using the Swift print statement or NSLog then it will be displayed in UTC. Always.
The code you've posted that prints strDate, your date string you created using a date formatter, is correct. The line `print(localDate!) is meaningless.
To repeat: NSDate objects do not have a time zone. When you log them to the console, they will always be displayed in UTC. If you want to see them in your local time zone then you need to use date formatter like you are doing.
I get current time or time stamp of some image. I have to change only date while the time should not be changed. For example I use [NSdate date] to get current date and time and store in an NSdate object that is "2014-01-10 09:58:47 +0000". Now change only the date part, keeping the time same as it is "2013-11-09 09:58:47 +0000"
How can I achieve that?
Convert the date into it's date components (which includes the time part), change the date part of the components to be for the new day, and create a new date based on these components.
dateByAddingDateComponents is also another way to do it.
It's all described in the Calendrical Calculations documentation.
If the user is in Eastern time, I don't want to display "EST" or "EDT" but if they are not, I do want to display it.
Is there an easy way to compare TimeZone rules in iOS?
[NSTimeZone localTimeZone] returns America/Indianapolis, but I just want to know if it is eastern.
Comparing data does not work, do I just compare secondsFromGMT?
EDIT
Comparing offsets does seem to work, but I don't know if it is going to cause problems later?
You can use [[NSTimeZone localTimeZone] abbreviation]; to get the abbreviation of whatever time zone the user is in.
This gives the abbreviation of the current date (EDT if not daylight savings and EST if daylight savings, in your case). If you want the abbreviation of a specific date, you can use abbreviationForDate: and insert any date.
Here is some more information about NSTimeZone
Edit:
If you want to actually compare multiple time zones (as in to check if it is the current time zone or not), you can use isEqualToTimeZone:. If that does not fit your needs, look at some of the other NSTimeZone methods in that link.
I've had lots of pain from dealing with time zone issues on our tools app, that includes event scheduling across time zones. The time zone names have not been friendly to use. Also, Daylight savings time is a great big pain.
What has worked consistently for me is calls like this using timeZoneForSecondsFromGMT
I also convert my stored dates to GMT, then I can set them to the local time on display.
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger timeZoneOffset = [zone secondsFromGMT];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeZoneOffset]];