This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I want to get the current date including the milliseconds. I know that Date.now() is returning the Unix epoch time in milliseconds, but the object Date contains only the Date and timestamp with second precision.
console.log("Time: " + new Date);
Time: Tue Apr 14 2020 12:21:26 GMT+0200 (Central European Summer Time)
But I want it to look like this:
Time: Tue Apr 14 2020 12:21:26.500 GMT+0200 (Central European Summer Time)
Is there a way to change the format in order to display the milliseconds as well or do I need to concatenate the Date string with new Date().getMilliseconds();?
You can use moment.js to achieve the format you need, and the other option is to concatenate manually
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SSS'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script>
Related
This question already has answers here:
Javascript date format like ISO but local
(12 answers)
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 4 months ago.
I have a date like this
Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
I am running this function on this date
const date = Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())
Which returns
2022 9 29 2 0 0
Is it possible to convert these numbers to
2022-10-29T02:00:00.000
the whole point of this is to use a library like moment-timezone and parse 2022-10-29T02:00:00.000 to a new timezone for example new_york. Im doing this to get the UTC time for 2022-10-29T02:00:00.000 in new york. The issue with new Date(2022-10-29T02:00:00.000) is that it always converts it to my local timezone.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I would like to simplify the display of a date (2016-06-26 05:23:55) which on discord.js gives this:
Sun Jun 26 2016 05:23:55 GMT + 0200 (Central European Daylight Time)
I would like to have this : Saturday June 26, 2016 at 05:23
I'd suggest using a library such as date-fns to format the date.
have a look at their docs: https://date-fns.org/v2.16.1/docs/format
You don't have to, but you'll have to it manually (array of months, pick Xth month, ...)
This question already has an answer here:
Initialize JS date object from HTML date picker: incorrect date returned
(1 answer)
Closed 3 years ago.
When I take in 3/15/2019 as the input date and then read back the time with console.log(fromDate) I get Thu Mar 14 2019 19:00:00 GMT-0500 (Central Daylight Time).
Why is it not Fri Mar 15 2019 00:00:00 ?
Is it because of the time on my local computer?
Is there a way to achieve the desired output?
fromDate = new Date(document.getElementById("fromDate").value);
This happens because new Date consider the date as UTC, so
var date = new Date('3/15/2019')
creates the date Fri Mar 15 2019 00:00:00. When you print the string however it considers your actual timezone, which is GTM-0500, so it removes 5 hours from the original date, yielding Thu Mar 14 2019 19:00:00.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
From service I get date string in format 'yyyy-mm-dd' (eb, '2017-09-14')
In JavaScript, I convert this string to Date like as mentioned below:
new Date('2017-09-14')
It works just fine. But I am facing problem in different time zones. For example
In EST Timezone: Wed Sep 13 2017 20:00:00 GMT-0400 (Eastern Daylight Time) and
In IST Timezone: Thu Sep 14 2017 05:30:00 GMT+0530 (India Standard Time)
Is there a way to convert it to the same date e.g. Sep 14, 2017
YOu can do it like this
var d=new Date(date value);
var actualDate=d.getDate();
var actualMonth=d.getMonth()+1;
var actualYear=d.getFullYear();
var formattedDate=actualDate + '/' + actualMonth + '/' + actualYear;
You can save it to any format
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
These are my two codes:
var date1 = new Date('2017-04-23');
var date2 = new Date('April 23, 2017');
console.log(date1);
console.log(date2);
this is the results:
Sat Apr 22 2017 17:00:00 GMT-0700 (PDT)
Sun Apr 23 2017 00:00:00 GMT-0700 (PDT)
why is date1 showing as the 22nd at 17:00?
JavaScript's Date parsing behavior is somewhat unreliable. It seems that when you give it an ISO 8601 string such as `"2017-04-23" it interprets the date as being in your own timezone, but when you give it an arbitrary string, it will interpret it as a UTC date.
Since you are in the GMT-7 timezone, the 22nd at 17:00 is the 23rd at 00:00 in UTC, and when you print out a date object, it will always print out the UTC date and not the localized date.
So, in summary, both dates are getting set to the 23rd at 00:00, but in different timezones. The first is being set to Apr 23 00:00 UTC-7 and the second one is being set to Apr 23 00:00 UTC.
It might be a good idea to always explicitly set a timezone in order to avoid this ambiguity.