Convert a dd/mm/yyyy string to a date in javascript

Fri Oct 23 2015 15:24:53 GMT+0530 (India Standard Time)

Bạn đang xem: Convert a dd/mm/yyyy string to a date in javascript



Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
Help us improve our answers.

Are the answers below sorted in a way that puts the best answer at or near the top?

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.

DD/MM/YYYY format - manually

If you work with this format, then you can split the date in order khổng lồ get day, month and year separately and then use it in another constructor - Date(year, month, day):

For more information, you can read article about Date at Mozilla Developer Network.

DD/MM/YYYY - using moment.js library

Alternatively, you can use moment.js library, which is probably the most popular library khổng lồ parse & operate with date and time in JavaScript:

In all three examples dateObject variable contains an object of type Date, which represents a moment in time & can be further converted to lớn any string format.


Here"s one I prepared earlier...

convertToDate(dateString) // Convert a "dd/MM/yyyy" string into a Date object let d = dateString.split("/"); let dat = new Date(d<2> + "/" + d<1> + "/" + d<0>); return dat;

Xem thêm: Trẻ Học Không Nhớ Chữ Cái - Làm Thế Nào Khi Bé Lớp Một Học Trước Quên Sau



function newUYDate(pDate) let dd = pDate.split("/")<0>.padStart(2, "0"); let milimet = pDate.split("/")<1>.padStart(2, "0"); let yyyy = pDate.split("/")<2>.split(" ")<0>; let hh = pDate.split("/")<2>.split(" ")<1>.split(":")<0>.padStart(2, "0"); let mày = pDate.split("/")<2>.split(" ")<1>.split(":")<1>.padStart(2, "0"); let secs = pDate.split("/")<2>.split(" ")<1>.split(":")<2>.padStart(2, "0"); mm = (parseInt(mm) - 1).toString(); // January is 0 return new Date(yyyy, mm, dd, hh, mi, secs);
I found the mặc định JS date formatting didn"t work.

So I used toLocaleString with options

const sự kiện = new Date();const options = dateStyle: "short" ;const date = event.toLocaleString("en", options);to get: DD/MM/YYYY format

See docs for more formatting options:

$scope.ssdate=date.toLocaleDateString();// mm/dd/yyyy format
Highly active question. Earn 10 reputation (not counting the association bonus) in order to lớn answer this question. The reputation requirement helps protect this question from spam & non-answer activity.

Not the answer you're looking for? Browse other questions tagged javascript date or ask your own question.

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day & month values get mixed resulting in NaN/Error
I am converting Json date into date object but due to lớn another timezone hệ thống time getting change. I want khổng lồ remain the date and time zone same
Site thiết kế / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.4.14.41981

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.