I am assuming that , If I run this query on
> (new Date()).getTime()
1443104144268
on Node console or JS console then I will get this timeStamp.
As per my understanding, irrespective of Location of console or timezone, this timestamp is always same. If at one particular instance, everybody in the world run this statement, then everybody will get same timestamp.
In my project, I need to convert this timeStamp (1443104144268) to YYYY-MM-DD (2015-09-24) format.
But I do not want to use any existing Date/Time library of System library.
Do anybody know how to convert this timestamp to YYYY-MM-DD (2015-09-24) format without using Date function in JavaScript. ?
Related
I'm trying to get the Timestamp value from firestore (using Firebase Functions), and I´ve successfully done it localy with the toDate() method of Timestamp, and moment library.
moment(doc.data().EndDate.toDate())
But when I deploy my code to firebase and test the function, somehow the toDate() returns a Date with 1 less hour than the saved timestamp on firebase. I suppose it is transforming my date to UTC, since I'm in UTC+1, and the Timestamp is also stored with UTC+1 in firestore, but I don't know how to reliably get the timestamp date as is in firestore, regardless of timezones.
If someone knows why this happens or has any idea how to solve it it would be great.
All timestamps in Firestore are stored in UTC. If you see something different in the Firebase console, that's just your browser formatting it for your local timezone.
In JavaScript, all Date objects are also represented in UTC. If you format that as a string, you will again possibly get a different representation based on your local timezone.
If you write code that computes values using dates or timestamps, you should perform all your computations using UTC. This is the pretty much all computing systems want to deal with dates. When it comes time to format the date for display to an end user, only then should you take timezone into account, and present something according to the user's preferences.
My app (back-end in C# & front-end in Angular Materials) has a search screen allowing user to specify the date period using datepickers. The problem is that some of the users are not in UK while all the data they view has been created with GMT date. So if someone in Germany selects date 01/01/2017 in datepicker, my back-end reads it as 31/12/2016 23:00:00 resulting in incorrect search results.
Can someone advise me how to deal with this? I'd like to still use the Angular Material datepicker but be sure that I'm passing the date selected by the user. I know I can transform the date before posting it like this:
moment(myDate).format('MM/DD/YYYY'))
but I have a lot of cases like this and would prefer some generic solution.
For transmission and storage, I advise using UTC for everything. Only at the point of display should the time be converted to whatever locale the user has selected. Despite this being an old problem, running into time conversion issues is still quite common. Most places I've worked at will store everything as UTC timestamps or Unix epoch time with respect to UTC, that way there is no question what the meaning is anywhere in the system. If/when it needs to be rendered to something local, we do it on the client side.
For example, to get the local time converted to UTC as a string:
var noTimeZone = new Date().toUTCString();
-or-
var noTimeZone = new Date().toISOString();
Or, if you want a numeric value so you don't have to deal with funky format parsing between client/server, you can get the Unix epoch:
var unixEpochMS = new Date().getTime();
Mind you, Date.getTime() will return milliseconds rather than seconds. Also note that the Unix epoch is defined in terms of UTC. That is, any numeric value that is a timestamp is expected to be UTC. If you want a different timezone, you need to parse the value and then set the timezone to what you want.
Solution 1:
I think the solution is to get your user's timezone. You can use Javascript to get timezone from user's computer and send it to server with the request.
var d = new Date();
var tz = d.getTimezoneOffset()/-60;
tz will be 2 if user's timezone is GM+2
Soution 2:
You send and receive Unix timestamp. But then you need to convert the timestamp to readable date/time based on user's timezone.
I'm attempting to use moment.js to format Apache log timestamps (14/Jun/2015:11:05:54 -0700) as proper MySQL DATETIME format.
Getting the MySQL DATETIME format is a breeze. But moment seems to only auto-detect ISO 8601 formats. My Node.js console output when passing in the Apache log's timestamp into moment:
> moment( '08/Jun/2015:15:03:29' ).format("YYYY-MM-DD HH:mm:ss")
'Invalid date'
I can't figure out a way to parse the Apache log timestamp without doing a ton of string splitting in JavaScript. Is there a clean way of loading the Apache log timestamp into moment? Or perhaps into the Date object?
NOTE: Changing the Apache log's timestamp format wouldn't work for my case, because I'm working with old log files, not new ones.
You need to tell moment what format the string is in so it can parse it.
Something like:
moment( '08/Jun/2015:15:03:29', 'DD/MMM/YYYY:HH:mm:ss Z').format("YYYY-MM-DD HH:mm:ss")
I'm facing some date time formatting related issue.
I'm confused about how a date object's output string is formatted. I did some testing in debug, when I call the toLocalString, the output is not following locale settingin the OS .
Below is the output of the method:
"1/12/2015, 8:12:12 PM"
But what I did in the os locale setting is
Why is toLocaleString formatting the date this way? where does those format coming from?
Where to change the format setting browser is using?
Why is toLocaleString formatting the date this way?
toLocaleString() doesn't watch for user's locale formatting settings before returning the string.
Where does those format coming from?
The format is based on the conventions of user's time zone for representing date and time. So, format is machine independent.
Where to change the format setting browser is using?
As stated the format is implementation dependent. It won't help you anything. And I think browsers don't provide such functionality.
For reference I have included it's documentation below.
The Documentation of Date.toLocaleString() as mentioned in Javascript: The Definitive Guide says:
Returns
A string representation of the date and time specified by date. The date and time are repre- sented in the local time zone and formatted using locally appropriate conventions.
Usage
toLocaleString() converts a date to a string, using the local time zone. This method also uses local conventions for date and time formatting, so the format may vary from platform to platform and from country to country. toLocaleString() returns a string formatted in what is likely the user’s preferred date and time format.
We are displaying schedules on our webpage which is build on GWT. Client system using different timezone from server and because of that, all the schedules were displaying wrong. Is there a way to set default time zone when we load the page? Like the way we do it in java:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
Thanks!!!
No, you can't set the timezone of Date objects in javascript. Usually you use only UTC and epoch-based timestamps.
Only when creating a Date from a string or from year, month etc. the local timezone will be used, you can only get the timezone offset.
Converting a timezone can only be done by re-setting the Hours of the Date object (example described here), creating a date which looks-like having an offset timezone but is just utc.
In case you are using moment.js for your dates, you can set the default timezone for all newly created moments with:
moment.tz.setDefault(String)
https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/