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.
Related
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
For 2015,July,3 it is Friday, but my app on some user's iphone devices just return Thursday,which is obviously wrong.
Just wonder why this happened ? How to fix it ?
Thanks,
We need use NSDateFormatter to return the weekday for given date. In my case, the given date is converted to UTC, but didn't set the time zone as UTC, that will cause problem to display wrong weekday in some country with different time zone.
So the fixing solution is to set up the date formatter with correct time zone.
formatter.timeZone = NSTimeZone(abbreviation: "UTC") // my case date is UTC
I know that NSDate doesn't have timezone information.
However, I'm trying to understand how to manipulate them properly.
At the moment I'm passing a date into an object. That date is the user selected date at time 00:00:00.
i.e. if the user hits October 21st then the NSDate passed in should be. 21/10/2013 00:00:00.
However, it isn't it's 20/10/2013 23:00:00. (One hour before).
Now, this is nothing about formatting them or displaying them. I'm just using the NSDates.
I'm creating the date using NSDateComponents and NSCalendar.
I guess my question is...
How can I tell what date an NSDate is actually referring to in my local time zone?
I need to send a UNIX time stamp for 00:00:00 and 23:59:59 for a given date. However, at the moment when I set the hour, minute and second to 0, 0 and 0 then I'm not getting midnight in the current time zone I'm getting midnight in GMT.
This isn't what I want.
Fixed?
OK, I've fixed it... I think. At least, it's doing what I want it to do.
The trick is...
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateComponents setSecond:timeZone.secondsFromGMT];
I've been confused by this many times. When you NSLog an NSDate, you'll always get the output in GMT. So the 20/10/2013 23:00:00 (GMT) you're seeing is the same as your expected 21/10/2013 00:00:00 (BST). The UNIX timestamp for both of these dates would be the same because it doesn't take into account timezone - it's always UTC.
If you want to output in a user-readable format, an NSDateFormatter will format the date using your current timezone and locale.
I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.
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.