I am getting some results from a weather API and one of that is date in epoch time stamp.
I found that converting with Date(timeIntervalSince1970:) I get the right date
I am using the specific number --> 1501452000 and I get 2 results on Playground
1) Jul 31,2017,12:00AM. -- when --> let date = Date(timeIntervalSince1970: 1501452000)
2) 2017-07-30 22:00:00 +0000 when --> print(date)
API results are :
"time_epoch": 1501452000,
"time": "2017-07-30 23:00",
By checking the rest of my results they are matching with the rest of the API results....... but when I convert 1501452000 -> to date I don't get the correct Hour 23:00 but 22:00 !
Any idea what is happening ?
is it wrong the API( I don't think so ) or the way I am converting it?
Thanks a lot
The timeIntervalSince1970 initializer sets up the time in the UTC timezone, while your API might be sending dates in GMT. When you are using print(data), you have different results, because if you are not using a DateFormatter to generate the String format of the Date object, it uses your devices current settings when formatting the Date object.
A Date object represents an absolute point in time, but when you are printing it with a DateFormatter, it gets converted into a location/time zone specific, relative representation. You just have to set up your DateFormatter to match the time zone settings of your API and you will see the dates correctly printed.
This issue happens on daylight saving times. Is your country changing daylight saving on this exact date?
let date = Date(timeIntervalSince1970: 1501452000) in Playgrounds should give you the time in your system's timezone when you see it on the right hand side.
When you print it and see 2017-07-30 22:00:00 +0000- this is the same timestamp in GMT
Is the API showing a particular time zone? It looks like GMT+1
Related
I have an todo application and some tasks have deadlines. If deadline is bigger or equal than current date, I want to display them. However, there is a local datetime problem i believe.
This is the current date using 'print(Date())':
2021-07-27 18:53:03 +0000
This is the beginning of that date using 'print(Calendar.current.startOfDay(for: Date()))':
2021-07-26 21:00:00 +0000
As you see, 27th day becomes 26th. I tried timezone operations also. It seems true but day is still starting at 21.00
When I convert them as string and print them, it is ok it show the expected results but without string type it becomes like above. Because of I want to get the bigger or equal days compared to current date , string solution is not working. It is the 27th of day but because of date object it is shown as 26th day and my app showing yesterdays tasks etc.
Can you provide a help for me?
Thanks
i am currently having the following problem with relation to daylight saving.
Using the UI the user creates an entry for a specific date and time.
eg.: on 20.10 (so DST, this is the time where she is using the app) she creates an entry for 28.10 11:00 (this is outside the DST)
we create a date object like this, the format is the one used in the UI:
let dateFormatter = DateFormatter.getFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy HH:mm"
self.startDate = dateFormatter.date(from: label.text!)!
no further processing is done and the date object is saved locally in realm.
printing the object shows 2020-10-28 09:00:00 +0000 which is correct, since we are in CET (GMT +2 in summer).
As long as the device's date is before 25.10 (so in DST) the date is correctly displayed throughout the app.
Now, after the DST (starting with 25.10) the app wrongly displays 28.10 10:00.
We are now in winter time which is GMT +1
it seems that swift wrongly uses the DST setting for the time when it is called, rather then the actual datetime that is being converted.
what am i doing wrong? any other technology that we used would correctly display the time, as long as the timezone is the same.
I have this date in actuall
2016-09-03 19:00:00 +0000
Now I am trying to convert it to String using a specific format like below
But what I am getting in return is not as desired. the formatter is adding on day to the given date like below
Is this standard behaviour ?
This is not standard behaviour. This happen because of the time zone difference. Set time zone proper
Set the timezone.
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation: #"GMT"];
When you hover over the date, you can see that it is showing UTC, whereas the formatter is automatically converting this to a local date. If your timezone is 5 hours ahead of UTC, then it will be the next day locally from that time.
I am trying to select a date from DatePicker. It works properly often but when i select future date, the picker sets itself to current date(as per my code of maxdate). In this date shows current date in label but at backend object it is actually 1 day minus of current date. e.g After auto set of picker to current date, it displays in label 09-Apr-2015 but in my object(nsdate) which is want to Post to server api, it shows 2015-04-08 18:30:00 +0000. Thus my object send 8 apr to the server. Kindly reply if someone has faced this problem.
This is not a problem. The date picker is giving you correct time but only in the other time zone (Appears GMT+5:30)
When you are sending the date in your API, convert it to NSString using NSDateFormatter of the default time-zone or time-zone of your choice.
Some thing is wrong with your GMT settings.
try this code to fix:
[yourdataobject dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
This will automatically fix your date in whatever zone your app is running in.
I am working on NSDate and i am new for it.I have start date and end date,getting from user.And it is 2013-01-01 and 2013-02-19.When i try to display in console it is showing me 2012-12-31 18:30:00 +0000.So you can say 5:30 is gap.So i am adding time interval
startDate = [aStartdDate dateByAddingTimeInterval:19800];
endDate = [aEndDate dateByAddingTimeInterval:19800];
My question is when i am trying to get current week using 2012-12-31 18:30:00 +0000 date, it is showing me correctly.But when i use 2013-02-01 00:00:00 +0000 date,it is showing me total number of weeks in month.Please help me.Thanking you.
Timezones shouldn't be part of your model - They are a presentation problem.
Your model should always use a common default timezone. If you present the date to your users, apply a NSDateFormatter that uses a specific timezone.
In your case this means, that you shouldn't try to fix your dates by applying arbitrary intervals but use a date formatter in the final step (output) instead.