Parsing a string to a date in JavaScript
The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.
Examples of ISO format: YYYY-MM-DD
or YYYY-MM-DDTHH:MM:SS
.
But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.
To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z')
.
To display a date in UTC, use .toUTCString()
,
to display a date in user's local time, use .toString()
.
More info on MDN | Date and this answer.
For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00')
. Note that the number of the month must be 1 less.
Alternate method - use an appropriate library:
You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.
How to convert string to date object in javascript?
You can do it like that:
let date_string = "2019-06-12 12:02:12 232"
let [y,M,d,h,m,s] = date_string.split(/[- :]/);
let yourDate = new Date(y,parseInt(M)-1,d,h,parseInt(m),s);
console.log(`yourDate ${yourDate}`);
In addition, avoid using Date.parse()
method.
UPDATE:
By using the above approach, you will avoid some strange bugs as described in the above link. However, it is possible to use a great library Moment.js
:
moment("12-25-1995", "MM-DD-YYYY");
In addition, you could check if a date is valid:
moment("whether this date is valid").isValid(); // OUTPUT: false
How can I convert string to datetime with format specification in JavaScript?
I think this can help you: http://www.mattkruse.com/javascript/date/
There's a getDateFromFormat()
function that you can tweak a little to solve your problem.
Update: there's an updated version of the samples available at javascripttoolbox.com
How to convert dd/mm/yyyy string into JavaScript Date object?
MM/DD/YYYY format
If you have the MM/DD/YYYY
format which is default for JavaScript, you can simply pass your string to Date(string)
constructor. It will parse it for you.
var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();
Convert string to date in JavaScript
It's pretty simple:
var myDate = new Date("2013/1/16");
That should do the trick.
Have a look at the documentation for the Date object
how to force Date.parse() in javascript to convert to local time and date instead of UTC
By the ECMAScript spec, you simply need to use a T
instead of a space to separate the date and time parts.
const t = Date.parse('2021-02-22' + 'T' + '13:00');
console.log(t); // result will differ depending on local time zone
Related Topics
Why Does a Regexp With Global Flag Give Wrong Results
Methods in Es6 Objects: Using Arrow Functions
Download a File by Jquery.Ajax
How to Access the Matched Groups in a JavaScript Regular Expression
When Should I Use a Return Statement in Es6 Arrow Functions
Is It Safe to Expose Firebase APIkey to the Public
Web-Scraping JavaScript Page With Python
Sorting Object Property by Values
Add a Property to a JavaScript Object Using a Variable as the Name
Ways to Deal With #Document Under Iframe
How to Access the Contents of an Iframe With JavaScript/Jquery
Why Is Settimeout(Fn, 0) Sometimes Useful
How to Format Numbers as Currency Strings