xxxxxxxxxx
function getDate(date) {
let mydate = new Date(date);
let day = mydate.getDate()
let month = ["Januari", "Februari", "Maret", "April", "Mei", "Juni",
"Juli", "Agustus", "September", "Oktober", "November", "Desember"
][mydate.getMonth()];
return day + ' ' + month + ' ' + mydate.getFullYear();
}
xxxxxxxxxx
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today = new Date();
console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
// For custom format use
date.toLocaleDateString("en-US", { day: 'numeric' })+ "-"+ date.toLocaleDateString("en-US", { month: 'short' })+ "-" + date.toLocaleDateString("en-US", { year: 'numeric' }) // 16-Nov-2019
xxxxxxxxxx
const t = new Date();
const date = ('0' + t.getDate()).slice(-2);
const month = ('0' + (t.getMonth() + 1)).slice(-2);
const year = t.getFullYear();
const hours = ('0' + t.getHours()).slice(-2);
const minutes = ('0' + t.getMinutes()).slice(-2);
const seconds = ('0' + t.getSeconds()).slice(-2);
const time = `${date}/${month}/${year}, ${hours}:${minutes}:${seconds}`;
output: "27/04/2020, 12:03:03"
xxxxxxxxxx
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today = new Date();
console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options));
xxxxxxxxxx
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
hours = hours ? hours : 12; // the hour "0" should be "12"
minutes = minutes < 10 ? "0" + minutes : minutes;
var strTime = hours + ":" + minutes + " " + ampm;
return date.getDate() + "/" + new Intl.DateTimeFormat('en', { month: 'short' }).format(date) + "/" + date.getFullYear() + " " + strTime;
}
var d = new Date();
var e = formatDate(d);
console.log(e); // example output: 11/Feb/2021 1:23 PM
xxxxxxxxxx
// Many options with Intl.DateTimeFormat
const formatter = new Intl.DateTimeFormat('en', {
hour12: true,
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
});
formatter.format(new Date());
xxxxxxxxxx
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today = new Date();
console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016
Run code snippet
xxxxxxxxxx
const dateStr = '2020-06-21T10:15:00Z',
[yyyy,mm,dd,hh,mi] = dateStr.split(/[/:\-T]/)
console.log(`${dd}-${mm}-${yyyy} ${hh}:${mi}`)
xxxxxxxxxx
var todayDate = new Date().toISOString().slice(0, 10);
console.log(todayDate);
Run code snippet
xxxxxxxxxx
const d = new Date('2010-08-05')
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d)
const mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d)
const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d)
console.log(`${da}-${mo}-${ye}`)
xxxxxxxxxx
// Custom function to format date in following format
// dd-mm-yyyy
// dd/mm/yyyy
// dd.mm.yyyy
function dateFormater(date, separator) {
var day = date.getDate();
// add +1 to month because getMonth() returns month from 0 to 11
var month = date.getMonth() + 1;
var year = date.getFullYear();
// show date and month in two digits
// if month is less than 10, add a 0 before it
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
// now we have day, month and year
// use the separator to join them
return day + separator + month + separator + year;
}
var now = new Date();
console.log(dateFormater(now, '/'));
console.log(dateFormater(now, '-'));
console.log(dateFormater(now, '.'));