How to customize `serverTimestamp()`? - firebase

I want to customise setTimestamp().
firebase.firestore.FieldValue.serverTimestamp()
Currently the output is like August 27, 2019 at 3:38:55 PM UTC+5:30. What to do if I want the result to be August 27, 2019 only ?

When a timestamp type field is written to Cloud Firestore, it is just storing a value that describes a moment in time that's the same for all people on earth. What you're seeing in the console is just format that the console is using to make that readable by people. The timezone is represented in your computer's configured timezone.
If you're reading that timestamp in your code and want to format it for display in your app, you'll need to use some date formatting library to make that easy. It looks like you might be using JavaScript, so consider using a library such as momentjs to help with that. You will likely have to convert the timestamp to a Date type object first.

Firebase will always set a timestamp in a consistent way.
A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.
As the output you have given has a timezone, I'm guessing this has been applied elsewhere in your code when generating a Date object.
With this date object it is easy enough to reformat to match your desired display. An easy way if you are using angular is to use the date pipe
{{ dateObj | date }} // output is 'Jun 15, 2015'
{{ dateObj | date:'medium' }} // output is 'Jun 15, 2015, 9:43:11 PM'
{{ dateObj | date:'shortTime' }} // output is '9:43 PM'
{{ dateObj | date:'mm:ss' }} // output is '43:11'

In your template you can call .toDate() method of your timestamp field. Do something like:
{{response.data().updatedAt.toDate() | date}}
The date pipe supports multiple formats:
{{response.data().updatedAt.toDate() | date:'short'}}
{{response.data().updatedAt.toDate() | date:'long'}}
...
See datePipe documentation.

Related

Kibana: how to convert {{date}} in not UTC format

Elastic/kibana. I created a alert rule and try to use {{date}} (or {{context.date}}) in my emails.
Right now I am getting the date and time in UTC format like this: 2023-02-01T08:30:59.069Z
How to convert this datetime to another format in local timezone (+2): 2023-02-01 10:30?
I used: {{dateVar | dateformat="MM/dd/yyyy"}}, {{sysdate}} and another methods but...
If you are observing your data from Kibana you can update the timezone and time format from:
Stack management => Advanced Settings

Chaining together 2 Airflow macros

There are two Airflow macros available currently: ds_add and ds_format.
I would like to know the syntax for using them both.
For example, I currently can use one of them like this to add 7 days to a date:
dt7 = '{{ macros.ds_add(ds, 7) }}'
However, I actually need to do something like this and get back a YYYYMMDD format without using datetime or any other python package, since I need to feed this into an Operator:
dt7_fixed = '{{ macros.ds_add(ds_nodash, 7) }}'
But ds_add does not support 'YYYYMMDD' format, only 'YYYY-MM-DD'.
A workaround is to use ds_format in that one-liner too, but I can't grok the right syntax.
I believe this will do what you want:
{{ macros.ds_format(macros.ds_add(ds, 7), '%Y-%m-%d', '%Y%m%d') }}
You should think of these macros the same way you would think of functions.
As per the link you included above ds_format takes in 3 params, date string and two strings representing desired input and output format.
The output of the originally used ds_add macro is a string which you can use as the first argument here.
Note in the source that this uses datetime under the hood.

Format UTC Time To Local Time

When you have a string that is formatted in UTC Time, how can I format it to a DateTime but local time?
For example, if I have the below code, it improperly formats my code (meaning incorrect time)
string dateformatted = "2017-01-10T11:13:00-07:00"
DateTime.Parse(Convert.ToString(dateformatted));
However, the output from this is
01/10/2017 1:13:00 PM
Which is 2 hours ahead of the actual time of 11:13:00. How can I convert the string to the proper timezone time?
I believe you are looking for the Parse(String, IFormatProvider, DateTimeStyles) overload of the Parse method. The third parameter, DateTimeStyles, will allow for force or prohibit conversion between local and UTC times. Options include: AdjustToUniversal, AssumeLocal, and AssumeUniversal (among others).
According MSDN docs:
Return value Type: DateTime
An object that is equivalent to the date and time contained in s.
Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified. However, the Parse method
may also perform time zone conversion and set the value of the Kind
property differently,depending on the values of the s and styles
parameters:
If:
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| If | Time zone conversion | Kind property |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information. | The date and time is converted to the time in the local time zone. | DateTimeKind.Local |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information, and styles includes the AdjustToUniversalflag. | The date and time is converted to Coordinated Universal Time (UTC). | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains the Z or GMT time zone designator, and styles includes the RoundtripKind flag. | The date and time are interpreted as UTC. | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
You should add timezone.

moment.js and deprecated warning. timestamp to moment date object

I've read thru various posts on here in regards to similiar issues but none have solved my problem.
I manipulate the moment.js date object, and then store it as timestamp.
BUT, when I try to read in that timestamp again, I get that deprecated warning.
""Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info."
I've tried toDate(), format(), moment(myTimeStamp, 'ddd, DD MMM YYYY HH:mm:ss ZZ'); --> all generate the warning...
So, for example, my timestamp will look like this:
const timestamp = '1458586740000'
when I read that back and try to parse out the month/day/year, then the hour/min am/pm, etc... I need to get that timestamp into a moment.js object. Nothing is working for me. Any ideas.
How can I get this timestamp: '1458586740000', into a moment.js object so I can extract date date from it as I need?
EDIT: this is how I am storing the timestamp. So I would need to retrieve it from this.
let timeStamp = Moment(state[_Date])
.add({ hour: state[AMPM] === 'PM'
? +state[Hour] + 12
: state[Hour] ,
minute: state[Min] }).format('x')
The X token indicates a unix timestamp in seconds, and the x token indicates a unix millisecond timestamp (offset).
You appear to have a millisecond timestamp, so you would make a moment out of it by doing the following:
var a = moment('1458586740000', 'x')
It works without ' as well:
var a = moment(1458586740000, 'x')
You can also not specify the x and it should work:
moment(1458586740000)
Because you have a unix offset (milliseconds), not a unix timestamp (seconds), moment.unix is not what you want.
Then you can do the following:
a.format()
"2016-03-21T13:59:00-05:00"
Or you can use any of the other formatting tokens listed here to output whatever result you would like: http://momentjs.com/docs/#/displaying/format/
Based on the code you presented, I think you may be having problems because your timestamp is stored as a string (in ''). Parsing as a string causes an invalid date error, because it attempts to match ISO 8601 format and fails. Specifying that 'x' token will cause it to assume unix offset and work correctly.

Decoding flight Arrival / Departure time format

I've been using a real time flight traffic API lately and I'm stuck at formating the arrival / departure times from a json response.
They come like this:
{
"dep_schd":1426843500,
"arr_schd":1426849800,
"departure":1426844020,
"arrival":1426849221
}
It doesn't look like anything I've seen before. Any ideas on how to make these times readable?
As a bonus, I can give you how an estimated time arrival looks like:
"eta":1426849221
Thank you
Edit:
Okay guys, with a bit more research and the help from you guys I managed to convert my date to a human readable date like this:
var departure = $('#departure');
var departureDate = new Date(json.departure * 1000);
departure.html(departureDate.toUTCString());
Now what I get is something like this:
Fri, 20 Mar 2015 09:33:40 GMT
My question is how can I make it simpler? So as I can get something like this:
Fri, 20 Mar 2015 09:33
This is Unix time. The number of seconds since 01 Jan 1970, 00:00:00 UTC.
On Unix you can use functions like localtime or gmtime and strftime to convert it to a human-readable form. Most languages have similar functions for dealing with these unix timestamps.
To display it in another format, use the "get" functions on the Date object (since it looks like this is JavaScript). For example departure.getUTCHours().
A better solution though is to use a library like moment.js to format the date easily:
var mDeparture = new moment(departureDate);
departure.html(mDeparture.format('YYYY-mm-dd'));
The format which your are displaying is called Epoch and it is then converted into human readable format.Here is a online site where u can get this in readable format http://www.epochconverter.com/
But you didn't mention in which language you want to convert , every language as methods to convert this in human readable and then you have to pick which info you want from it.

Resources