How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript?
[Addendum 12/2022]: Here's a library to format dates using Intl.DateTimeFormat
.
Try something like this
var d = new Date,
dformat = [d.getMonth()+1,
d.getDate(),
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
If you want leading zero's for values < 10, use this number extension
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
// usage
//=> 3..padLeft() => '03'
//=> 3..padLeft(100,'-') => '--3'
Applied to the previous code:
var d = new Date,
dformat = [(d.getMonth()+1).padLeft(),
d.getDate().padLeft(),
d.getFullYear()].join('/') +' ' +
[d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
//=> dformat => '05/17/2012 10:52:21'
See this code in jsfiddle
[edit 2019] Using ES20xx, you can use a template literal and the new padStart
string extension.
const dt = new Date();
const padL = (nr, len = 2, chr = `0`) => `${nr}`.padStart(2, chr);
console.log(`${
padL(dt.getMonth()+1)}/${
padL(dt.getDate())}/${
dt.getFullYear()} ${
padL(dt.getHours())}:${
padL(dt.getMinutes())}:${
padL(dt.getSeconds())}`
);
Convert date object in dd/mm/yyyy hh:mm:ss format
You can fully format the string as mentioned in other posts. But I think your better off using the locale functions in the date object?
var d = new Date("2017-03-16T17:46:53.677"); console.log( d.toLocaleString() );
Format JavaScript date as yyyy-mm-dd
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
Javascript Date Now (UTC) in yyyy-mm-dd HH:mm:ss format
We should use in-built toISOString
function to covert it to ISO date format and remove not required data using string manipulation.
let datenow = new Date();
console.log(datenow); // "2021-07-28T18:11:11.282Z"
console.log(generateDatabaseDateTime(datenow)); // "2021-07-28 14:11:33"
function generateDatabaseDateTime(date) {
return date.toISOString().replace("T"," ").substring(0, 19);
}
How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
If you're using Node.js, you're sure to have EcmaScript 5, and so Date has a toISOString
method. You're asking for a slight modification of ISO8601:
new Date().toISOString()
> '2012-11-04T14:51:06.157Z'
So just cut a few things out, and you're set:
new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '') // delete the dot and everything after
> '2012-11-04 14:55:45'
Or, in one line: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
ISO8601 is necessarily UTC (also indicated by the trailing Z on the first result), so you get UTC by default (always a good thing).
How to Format Date to Complete YYYY-MM-DD-HH-MM-SS
You can get YYYY-MM-DD-HH-MM-SS format like this:
new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().split(".")[0].replace(/[T:]/g, '-')
Explanation:
.split(".")[0]
removes the dot near the end and everything after it..replace(/[T:]/g, '-')
changes theT
and the colons (:
) to dashes.
Related Topics
Angularjs - How to Get an Ngrepeat Filtered Result Reference
Disable Hover Effects on Mobile Browsers
Unsigned Integer in JavaScript
Unsafe JavaScript Attempt to Access Frame with Url
How to Completely Disable Any Mouse Click
Does Execcommand Saveas Work in Firefox
Why Can't We Have Return in the Ternary Operator
How to Detect Right Mouse Click + Paste Using JavaScript
How to Access Google Sheet Spreadsheets Only with JavaScript
Thickness of Lines Using Three.Linebasicmaterial
Why Are Objects Not Iterable in JavaScript
Does JavaScript Process Using an Elastic Racetrack Algorithm
What Does the @ Symbol Do in JavaScript Imports
Convert a Directory Structure in the Filesystem to JSON with Node.Js