Tuesday, May 3, 2022

Javascript display Date with format

module.exports={
getDisplayDate,
}

function getDisplayDate(dateObj){
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
let monthNumber=dateObj.getMonth()
return dateObj.getDate()+"/"+months[monthNumber]+"/"+dateObj.getFullYear();
}

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));

date. toLocaleDateString("en-US", options)



Option # 2

Use the date.format library:

var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");


Using Moment.js

var moment = require('moment'); // require
moment().format();

For Node.js


var format = require('date-format');
format.asString(); // defaults to ISO8601 format and current date
format.asString(new Date()); // defaults to ISO8601 format
format.asString('hh:mm:ss.SSS', new Date()); 

No comments:

Post a Comment