How do I format a date in JavaScript?
For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat
object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.
To do this, you can use DateTimeFormat#formatToParts
. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:
{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}
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'));
Format a date string in javascript
Use Moment.js and the .format
function.
moment('2017-06-10T16:08:00').format('MM/DD/YYYY');
Will output
06/10/2017
Beside the format
function Moment.js will enrich you will alot more useful functions.
How to change the format of a date in JavaScript?
Your date 2018-06-13T13:28:14+0000
is in UTC format.
One of the simplest options is to use momentjs to get desired date time format.
moment(yourDate).utc().format("DD/MM/YYYY hh:mm:ss a")
Below is code:
let date = moment("2018-06-13T13:28:14+0000").utc().format("DD/MM/YYYY hh:mm:ss a")
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
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 change a date format in JavaScript
Convert it do a Date
, get the make a string out of the components.
The month needs to add 1 since for some reason, months are zero-indexed (January == 0, February == 1, ..., December == 11)
const dateString = "13 Dec,2021 12:59:58";
const getNewString = (dateString) => {
const date = new Date(dateString);
return `${date.getDate()}-${date.getMonth()+1}-${date.getFullYear()}`;
}
console.log(getNewString(dateString));
Javascript format date / time
Yes, you can use the native javascript Date() object and its methods.
For instance you can create a function like:
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.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
var d = new Date();
var e = formatDate(d);
alert(e);
And display also the am / pm and the correct time.
Remember to use getFullYear() method and not getYear() because it has been deprecated.
DEMO http://jsfiddle.net/a_incarnati/kqo10jLb/4/
How to format an existing date from an Object in JS
You can write a simple utility function like below which will return you formatted date.
function getFormattedDate(date) {
var month = date.getMonth() + 1;
var day = date.getDate();
var year = date.getFullYear();
var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
return day + "." + month + "." + year + ", " + time ;
}
console.log(getFormattedDate(new Date()))
Related Topics
How to Make Internet Explorer Emulate Pointer-Events:None
Prevent Typing Non-Numeric in Input Type Number
Ios 8 Removed "Minimal-Ui" Viewport Property, Are There Other "Soft Fullscreen" Solutions
Change an Image With Onclick()
How to Get Value of a Div Using JavaScript
Set Webkit Keyframes Values Using JavaScript Variable
What Is the Cleanest Way to Disable CSS Transition Effects Temporarily
How to Build Simple Jquery Image Slider With Sliding or Opacity Effect
How to Force the Browser to Reload Cached CSS and JavaScript Files
Setting CSS Pseudo-Class Rules from JavaScript
How to Dynamically Create CSS Class in JavaScript and Apply
Accessing a CSS Custom Property (Aka CSS Variable) Through JavaScript
How to Change/Remove CSS Classes Definitions At Runtime
Getting Byte Array Through Input Type = File
Html5 Canvas Drawimage Ratio Bug Ios
Getting Binary (Base64) Data from Html5 Canvas (Readasbinarystring)