What is a simple way format date time to YYYY-MM-DD HH:MM:SS?
new Date().toLocaleDateString('en-CA') + ' ' + new Date().toLocaleTimeString('zh-CN')
We often need this ISO format string. Is there another way to do it that is simpler?
Related
I am running Nodejs in Lambda in Sydney region. I have below code:
> d=new Date('2020-12-14T13:20:44.733Z')
2020-12-14T13:20:44.733Z
> d.getDate()
15
It creates a Date instance with a date string 2020-12-14T13:20:44.733Z. But the date is translated to 15th of December not 14th. Does this mean the string 2020-12-14T13:20:44.733Z representing UTC time?
The process that is carried out is described in the specification here:
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Z is the UTC offset representation specified as "Z" (for UTC with no offset) or an offset of either "+" or "-" followed by a time expression HH:mm (indicating local time ahead of or behind UTC, respectively)
So the trailing Z means the time passed into the Date constructor is in UTC, but getDate will give you the local representation of whatever time is in the Date object.
I am getting a text value and it is formatted like this : "9/19/2018 10:00 AM".
How would you go about turning this into an epoch timestamp using moment.js or just javascript?
EXAMPLE:
console.log(moment('4/17/2018 10:00 AM', 'mm/dd/yyyy hh:mm a').format('x'));
Use the date string to create a date Object and then use getTime() method of the Object.
date = new Date("9/19/2018 10:00 AM");
console.log(date.getTime())
normal js
new Date("9/19/2018 10:00 AM");
if you want to do with moment js
moment("9/19/2018 10:00 AM").unix()
The issue in your code is that you are using wrong tokens while parsing your input. Moment token are case sensitive and mm stands for 0..59 minutes, dd stands for day name (according locale) and there is no lowercase yyyy supported.
Use M for 1..12 month number, uppercase D or DD for day of month and uppercase YYYY for year as stated in moment(String, String) docs.
Your code could be like:
console.log(moment('4/17/2018 10:00 AM', 'M/D/YYYY hh:mm a').format('x'));
console.log(moment('4/17/2018 10:00 AM', 'M/D/YYYY hh:mm a').valueOf());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Note that you can use both format('x') (returns a String) and valueOf() (returns a Number).
I suggest to use moment(String, String) over new Date() (Date.parse()) because this way you explicitly state how to parse you input (what would you expect for input like 1/2/2018? 1st of February or January 2nd?). See here a note about Date.parse behaviour
Somewhat surprisingly, these formats (e.g. 7/2/2012 12:34) are also unambiguous. Date.parse favors US (MM/DD/YYYY) over non-US (DD/MM/YYYY) forms.
These formats may be ambiguous to humans, however, so you should prefer YYYY/MM/DD if possible.
I have a time string in this format - 2015-09-17T16:00:00. How do I convert it to a timestamp format like this - 1447804800000 using Javascript/jQuery?
Use Date.prototype.getTime()
var x = new Date("2015-09-17T16:00:00").getTime();
alert(x);
I would like to know how to parse date strings that could have different date format.
For now I do the following to parse my date strings:
var parseDate = d3.time.format("%Y-%m-%d %H:%M").parse;
How to specify multiple date format to my parseDate function?
new Date('YOUR DATE STRING')
Also checkout moment.js,
You can use moment('YOUR DATE STRING')
moment will try to interpret the String according to standard formats, if not it will fall back to Javascript's native Date() constructor.
This should work for standard ISO format or a variety of commonly seen date formats. Obviously it is not magic and will not understand all possible permutations of YYYY MM and DD
Am trying to convert a string to date
var strdate='2014-04-23+09:06:57.4830591330'
while trying to convert this string to date using below code
var followupDate = new Date(strdate);
console.log(followupDate)
am getting the error
Date {Invalid Date}
You just need a space rather than an addition sign, so you could just replace it:
var strdate='2014-04-23+09:06:57.4830591330';
var followupDate = new Date(strdate.replace("+"," "));
console.log(followupDate);
Will log something like: Wed Apr 23 2014 09:06:57 GMT+0100 (GMT Summer Time).
You should replace the + sign in your string for a space, between the year and the hours.
To explain this, let's see Date documentation :
dateString
String value representing a date. The string should be in a
format recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).
Now, from RFC 2822 (under 3.3. Date and Time Specification), it's explicitly described that the + is meant for timezones:
zone = (( "+" / "-" ) 4DIGIT) / obs-zone
Moreover, note that there's no room for milliseconds in this standard.
Your String Format is Wrong
It should be like this
var strdate = "2014-04-23 09:06:57.4830591330"
if you are getting your value dynamicaly then change it to this format by
var newstrdate = strdate.replace("+", " ");
and then try
date = new Date(newstrdate);