I am building an browser-based application where the user sets the timezone of their application, e.g. "America/New_York". I have a part of the application that allows users to set reminders and I want the user to specify that they want, for example, to set a reminder for 28th Jan 2020 at 9am in their timezone. This should work such that it they are on holiday in London and used the application to set this reminder, displaying this reminder would still always be in the New York timezone. I store the dates as UTC in the applications database and when converting this back show the user will display it according to their timezone.
I have tried this method in Codesandbox, to convert the date to UTC to store in the database:
const dateInNewYork = dayjs("2020-01-28 09:00:00", { timeZone: "America/New_York" }).toISOString();
// displays as 2020-03-29T11:00:00.000Z - shouldn't this be 2020-01-28T14:00:00.000Z ?
What am I doing wrong here?
let timeSt = dayjs(time).unix()* 1000;
timeSt += 60*60*1000*8 //8 is means china beijing time.
let whatYouNeed = dayjs(timeSt).format("YYYY-MM-DD HH:mm");
Related
I'm using Apps Script and trying to extract data from AdminReports.UserUsageReport.get('all', 'the date') (to get classroom last interaction timestamps), but it always comes with records from another day. For example, if I extract from the 5th, the report brings the 6th together, until 7 o'clock in the morning.
The date parameter is a string and in the documentation says the following:
Represents the date the usage occurred. The timestamp is in the ISO
8601 format, yyyy-mm-dd. We recommend you use your account's time zone
for this.
But if the parameter is a string in yyyy-mm-dd format, how am I going to pass the date with the right time zone?
This code not work:
var myDate = new Date(2021,5,5);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(myDate, timezone, 'yyyy-MM-dd');
var page = AdminReports.UserUsageReport.get('all', date);
How do I use the date correctly?
Thanks a lot.
If the report dates are UTC, then each may first be converted to JavaScript Date objects using
var myDate = new Date('report_date_string');
The second two lines of your code look like they should follow correctly in converting and formatting the Dates to strings in your local time zone.
I am creating a social media app. You can only post once every 24 hours, and I'm using it with firebase so all of the post' data goes to the firebase database.
When creating a post I have the variable Date, this is how you track if it's been 24 hours since the last post...
The only problem is that the user can just change the clock on their device and post as many times as they want. I need to get a date variable that is the local time of my timezone (EST) so I know that the data is accurate, but I'm not sure how to do it...
This is how I would usually get the current date:
let date = Date()
let currentTime = date.timeIntervalSince1970
So, I just need to get currentTime to be the current Eastern Standard Time (EST)
Thanks in advance!
Get the current timestamp form the firebase server and compare to the date from the last post
firebase.database.ServerValue.TIMESTAMP
Go here for reference firebase.database
for firestore
firebase.firestore.Timestamp.now()
Go here for reference firebase.firestore
If you want to make sure that 24 hours have passed, you need to compare the date in UTC, not in EST, UTC is a date without any time zone
and here
let date = Date()
let currentTime = date.timeIntervalSince1970
you have UTC timestamp
UPD: the fact of publication itself should be checked exclusively on the server, while the server should take its UTC time and check the time of the last publication from the database, before publishing.
If you are not trusting the device time, you could probably use an NTP client to get the date through an internet connection.
There are some implementations written in Swift like TrueTime and Kronos.
Both appears to use by default time.apple.com.
Their respective guides (on README files) seems to be pretty straightforward.
Example using TrueTime:
let client = TrueTimeClient()
client.start()
client.fetchIfNeeded { result in
switch result {
case let .success(referenceTime):
// Current date
let now = referenceTime.now()
client.pause()
case let .failure(error):
print("Error: \(error)")
}
}
Example using Kronos:
Clock.sync { date, offset in
// Current date
print(date)
}
Bear with me, I'm a newbie to iOS development; I'm currently trying to get the current time in UTC based on the device's local time, and set it in an if / else statement based on the whatever the current time in UTC is.
I've tried the below:
let UTCdate = Date()
if (UTCdate >= 13 && UTCdate < 23 {
// do something
}
It's giving me the error that "Binary operator '>=' cannot be applied to operands of type 'Date' and 'Int'
I know I'm doing something wrong, just don't know what. Apologies if this has already been asked. I google'd to the best of my abilities.
You need to extract the hour from your date. Date is a timestamp, not an hour. And you need to make sure you extract the hour in the UTC timezone and not locale time.
The following will do what you need:
// Create a calendar with the UTC timezone
var utcCal = Calendar(identifier: Calendar.current.identifier) // or hardcode .gregorian if appropriate
utcCal.timeZone = TimeZone(secondsFromGMT: 0)!
// The current date
let date = Date()
// Get the hour (it will be UTC hour)
let hour = utcCal.component(.hour, from: date)
if hour >= 13 && hour < 23 {
}
If you just use the current calendar to extra the hour from date you will get the hour in local time. That is the reason we created a new calendar specific to the UTC timezone (since that is your requirement).
Based on some comments you may not actually want UTC. Simply set the timeZone to whatever timezone you actually need.
If you want Chicago time, use:
utcCal.timeZone = TimeZone(identifier: "America/Chicago")!
And an alternate to creating your own calendar instance is to use:
let hour = Calendar.current.dateComponents(in: TimeZone(secondsFromGMT: 0)!, from: date).hour!
Again, use whatever timezone you need.
You said "I'm currently trying to get the current time in UTC based on the device's local time". You seem not to understand a key thing about Date objects in Cocoa. A Date is an instant in time, anywhere on the planet. It doesn't have a time zone. Internally it's represented as an offset from a moment in time that's expressed in UTC, known as the "epoch date", but that's an implementation detail. Imagine that when I capture a Date using the call Date(), I snap my fingers and the sound of the finger snap is heard all around the world at the same instant, without even speed-of-light delay. The finger snap is the moment that the call to Date() captures.
If I use the code let date = Date() I capture the current time all over the planet. A Date object doesn't have a specific time of day unless I convert it to a specific time zone. As others have suggested, the Calendar method dateComponents(in:from:) will let you extract components like the hour in a specific time zone from a Date, but you need to understand how all this works or you're going to get confused.
I suggest searching on the phrase "Calendrical Calculations" in the Xcode help system and reading that section of the docs. It's very helpful.
I am need to format a date and display it to users based on users location.
I am trying to format the time using the following code
DateTimeFormat.getFormat("h:mm a z").format(new Date(timeInMillis))
This is the result i am getting "5:18 PM UTC-4" for new york users and "2:18 PM UTC-7" for seattle users. How do i generate string like "5:18 PM EST" for new york users and "2:18 PM PDT" for seattle users?
Note: the problem with using format(new Date(time), timezone) is that how to create a timezone object based on user locale? Timezone.createTimeZone(int) gives SimpleTimeZone implementation which will produce "UTC-4", in-order to generate "PDT", timezone has to be created with Timezone.createTimeZone(timezoneJson: string) but issue in this is that we have to pick the timezone at the compile time to create the input timezone json string.
Thanks in advance
You need to pass a TimeZone object to the formatter:
format.format(new Date(), timeZone));
Note that the best way to create TimeZone is from a JSON string that contains information on changes to time zone in the past. If you app does not deal with time in the past, then this may not be necessary.
I am able to get list of timezones using
NSArray *timezoneNames = [NSTimeZone knownTimeZoneNames] ;
The issue is that the list is generated based on the current date set on device. I want this list to be generated using a future date, so that it correctly reflects the applicable Daylight Saving Offset.
e.g. If I use the APP in May 2012 the timezone entry for Chicago is
America/Chicago (CDT) Offset -18000 (Daylight)
and if I use the same APP in February 2012 the entry for Chicago is
America/Chicago (CST) Offset -21600
Can I change the System date within my code so that I can generate the list for any particular date at will?
Sanjay