Get beginning of the day by given date in Swift - ios

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

Related

Google Sheets: IF and Date formula not working

I'm calculating annual holiday accrual.
All holiday resets at the start of the calendar year.
If a staff member started after 01.01.2017 they will have a lower holiday entitlement for the year.
I'm trying to create a formula which says:
If this cell's date is after 01.01.2017 return that cells date. Otherwise, set it at 01.01.2017.
(so basically, I don't care when they started if it was before 01 Jan 2017 because all my following calculations will be based off the first day of the year)
Here it is:
=if(T21<date(1,1,2017),T21,"01/01/2017")
No matter what is in the cell, it is returning 01/01/2017.If I change the < to > it returns cell T21 in all cases.
Any ideas?
Thanks
You have the DATE() values mixed up. Try:
=if(T21<date(2017,1,1),T21,"01/01/2017")
DATE() is year, month, day
Also, you may want to use DATEVALUE() on the FALSE value to force the returned item to a date:
=if(T21<date(2017,1,1),T21,DATEVALUE("01/01/2017"))
"01.01.2017" is not a valid date in Google docs. You need to change . to /.
=DATEVALUE(SUBSTITUTE(T21,".","/"))
=IF(DATEVALUE(SUBSTITUTE(T21,".","/"))<DATEVALUE(date(2017,1,1)),T21,"01/01/2017")

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

Datepicker date format issue

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.

Timezone Offset in Angular JS and Rails

Background: I'm building an app with Angular JS as web interface and Rails API. The problem I am having is passing a date from Angular to Rails.
Issue: I have a form with a Date of Birth date field, when a user inputs his DOB say March 1st, 1985, Angular interprets it as 1985-03-01 00:00 +0800 (if you're in Hong Kong or Singapore) and sends a request to Rails. The first thing Rails does with it is to convert it to UTC, which means the datetime is now 1985-02-28 16:00 UTC. Therefore, when the date is saved to the database date column, it becomes Feb 28, 1985.
Solution for now: What I'm doing now is on Angular side, I get the Timezone offset hours and add it to the date, so instead of 1985-03-01 00:00 +0800, it is now 1985-03-01 08:00 +0800. When Rails get it, it converts to 1985-03-01 00:00 UTC and so saves the correct date to db. However, I believe this is a better alternative to tackle this issue.
Thinking about parsing just the date in Rails, yet the params[:dob] I see is already UTC by the time I get it. Would love to know if there is a better practice than my current solution. Thank you for any comment and feedback.
This problem is actually quite common, and stems from two separate but related issues:
The JavaScript Date object is misnamed. It's really a date + time object.
The JavaScript Date object always takes on the characteristics of the time zone for the environment in which it is running in.
For a date-only value like date-of-birth, the best solution to this problem is to not send a full timestamp to your server. Send just the date portion instead.
First, add 12 hours to the time, to use noon instead of midnight. This is to avoid issues with daylight saving time in time zones like Brazil, where the transition occurs right at midnight. (Otherwise, you may run into edge cases where the DOB comes out a day early.)
Then output the date portion of the value, as a string in ISO format (YYYY-MM-DD).
Example:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
var pad = function(n) { return (n < 10 ? '0' : '') + n; }
var dob = dt.getFullYear() + '-' + pad(dt.getMonth()+1) + '-' + pad(dt.getDate());
Another common way to do this is:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
dt.setMinutes(dt.getMinutes() - dt.getTimezoneOffset()); // adjust for the time zone
var dob = dt.toISOString().substring(0,10); // just get the date portion
On the Rails side of things, use a Date object instead of a DateTime. Unlike JavaScript, the Rails Date object is a date-only object - which is perfect for a date-of-birth.

Format times to display with 'AM/PM'

Normally, when I enter times in Google spreadsheet as 22:00:00 the display automatically switches to 10:00 PMbut sometimes it's not converting.
How can I set a common format for times in Google spreadsheet? I need this format: 8:00 PM.
Select all ranges that you wish to have this time format and go to Format > Number > More formats to select the one of your choice. (I think the one you want is near the bottom and shows as 3:59 PM.)
If you want to format a time with TEXT() to display with am/pm you can use:
=TEXT(DateField, "HAM/PM")
To get a results like
1am
1pm
10pm
12am
Use UPPER() to make it uppercase.

Resources