Turning a localized Date to a Timestamp in Firebase - swift

I'm new to Firestore and since it doesn't seem like it has a native createdAt/updatedAt as part of a document, I'm creating them when I create the new document. Using a straight up Date() as the value for my initial dictionary that I save to Firestore obviously gives me a localized date/time – UTC -500, for example.
Firestore stores this as November 19, 2018 at 5:14:54 PM UTC-5
Is there a specific date format that Firestore likes in order to save something as a Timestamp and so that I will be able to sort on it later?
I tried using "yyyy-MM-dd HH:mm:ss Z" as the dateFormat from just printing out the value of Date() in a Playground. Would love some help here. Thanks!

A Timestamp object doesn't have a date format. It's just a measurement of a number of seconds since Unix epoch, plus some number of nanoseconds. It does not accept a formatted time. If you have a formatted time, you will have to parse it to get a Timestamp object in return.`
The date format you're seeing in the console is just the way the console is choosing to format it. If you want to format a Timestamp for display, you'll need to do that yourself.

Related

Does the creation of a date object accept a parameter for a timezone?

To my understanding let date = Date() would create a date object with my current timezone. I've seen that there are certain functions where I would use a date formatter to then convert it into UTC time and add the offset to that time and then convert back into a date object but I'm wondering if there is a more elegant solution? And if not is there any other way to get the current time in another timezone in a date object without using the data formatter, as I require the calendar.component(.weekday, from: date) to find the date of the week.
If you look at the very first line on the documentation on Date, you can read the following:
A specific point in time, independent of any calendar or time zone.
So your two options when you want to deal with a local date are the DateTimeFormatter and Calendar

Google App Script - how to set date with timezone in AdminReports.UserUsageReport.get()

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.

Keeping local Timezone information from an ISO 8601, RFC3339 formatted dates in iOS

I know there are tons of post regarding iso8601 strings and timezones but I could not find anything that really pinpoints the problem I had recently.
I have an ISO 8601 RFC3339 formatted string like: 2021-03-31T12:00:00+03:00.
I want to display the time associated with this date in the local time of the provided date meaning I want exactly "12:00" as the output. If my formatter has dateFormat of HH:mm
If I use an ISO8601DateFormatter to extract the date everything seems to work fine and the associated Date object is 2021-03-31 09:00:00 +0000. Which makes sense since 12:00GMT+3 = 09:00GMT+0
However this completely removes Timezone information from the Date object (which I know is by design on iOS).
While I understand the design behind this (most of the time we eventually should display the time in the user device timezone). There are quite a few exceptions like travel applications where we almost alway want to display the local time of departure/arrival.
My solution was to store the json serialized dates as Strings and use a combination of ISO8601DateFormatter to create the Date object in UTC and a normal DateFormatter that reconstructs the TZ from the +03:00 substring.
What's the best approach to solve this ?

Date stored in different format

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app.
In Spring, the field date is as follows :
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
The date is well stored, but with this format YYYY-MM-dd.
Is there anything that I'm missing ?
MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.
You probably have to specify the date format going out to the storage, as #JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still
Formats a date in the date escape format yyyy-mm-dd.
according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.
You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

Date Column Split in Talend

So I have one big file (13 million rows) and date formatted as:
2009-04-08T01:57:47Z. Now I would like to split it into 2 columns now,
one with just date as dd-MM-yyyy and other with time only hh:MM.
How do I do it?
You can simply use tMap and parseDate/formatDate to do what you want. It is neither necessary nor recommended to implement your own date parsing logic with regexes.
First of all, parse the timestamp using the format yyyy-MM-dd'T'HH:mm:ss'Z'. Then you can use the parsed Date to output the formatted date and time information you want:
dd-MM-yyyy for the date
HH:mm for the time (Note: you mixed up the case in your question, MM stands for the month)
If you put that logic into a tMap:
you will get the following:
Input:
timestamp 2009-04-08T01:57:47Z
Output:
date 08-04-2009
time 01:57
NOTE
Note that when you parse the timestamp with the mentioned format string (yyyy-MM-dd'T'HH:mm:ss'Z'), the time zone information is not parsed (having 'Z' as a literal). Since many applications do not properly set the time zone information anyway but always use 'Z' instead, so this can be safely ignored in most cases.
If you need proper time zone handling and by any chance are able to use Java 7, you may use yyyy-MM-dd'T'HH:mm:ssXXX instead to parse your timestamp.
I'm guessing Talend is falling over on the T and Z part of your date time stamp but this is easily resolved.
As your date time stamp is in a regular pattern we can easily extract the date and time from it with a tExtractRegexFields component.
You'll want to use "^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}):[0-9]{2}Z" as your regex which will capture the date in yyyy-MM-dd format and the time as mm:HH (you'll want to replace the date time field with a date field and a time field in the schema).
Then to format your date to your required format you'll want to use a tMap and use TalendDate.formatDate("dd-MM-yyyy",TalendDate.parseDate("yyyy-MM-dd",row7.date)) to return a string in the dd-MM-yyyy format.