javascript date from string dd/mm/yyyy code example
Example 1: javascript convert date to yyyy-mm-dd
const formatYmd = date => date.toISOString().slice(0, 10);
formatYmd(new Date());
Example 2: javascript format date to dd-mm-yyyy
function pad2(n) {
return (n < 10 ? '0' : '') + n;
}
var date = new Date();
var month = pad2(date.getMonth()+1);
var day = pad2(date.getDate());
var year= date.getFullYear();
var formattedDate = day+"-"+month+"-"+year;
alert(formattedDate);
Example 3: convert date dd/mm/yyyy to date object js
DateParts = data.split("/");
new Date(+DateParts[2], DateParts[1] - 1, +DateParts[0]).setUTCHours(
0,
0,
0,
0
)
Example 4: get date format javascript
const handleDate = (dataD) => {
let data= new Date(dataD)
let month = data.getMonth() + 1
let day = data.getDate()
let year = data.getFullYear()
if(day<=9)
day = '0' + day
if(month<10)
month = '0' + month
const postDate = year + '-' + month + '-' + day
return postDate
}