Datepicker date format issue - ios

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.

Related

Get beginning of the day by given date in Swift

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

Date(timeIntervalSince1970:) returns 2 different results

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

How to get the date from an UIDatePicker with mode set to Time only

My UIDatePicker needs to show only the time, but I also need to get the current date when that time was set.
Is this possible if the date picker has the mode set to Time ?
Currently I get this when I print the current date from date picker:
2000-01-01 3:00:00 AM +0000
You can get current date creating a Date object "Date()", and you could use the "valueChanged" event to merge both values, the time they picked and the date you got

Getting next date when trying to convert NSDate to String

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.

NSDateComponent giving me day incremented by 1

I know this is a time zone problem, but I don't know how to solve this.
I have a NSDate = 2013-07-29 01:10:01 +0000
When I do a NSDateComponent, the day component shows 28!
I'm in EST time zone right now, but why doesn't it just show 29 straight up?? Is this a bug?
If not, how do i fix this to show it properly? Or is it already proper?
Thanks.
The problem is that your calendar instance is currently set to your time zone, so it is interpreting the date as 5 hours prior to what it would be in GMT (which is what you pasted in your question).
To resolve this issue you should set the timeZone property on the calendar you are using to get the date components out of the date. Something like this will probably do the trick, assuming your calendar variable is named calendar:
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

Resources