Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript date format method

Tags:

javascript

Having trouble with this:

var now = new Date();
var timestamp = now.format("dd/mm/yyyy HH:MM:ss") + " (" + GetLoggedUserName() + ")"; 

I get: Object doesn't support property or method 'format'

I was sure this worked before though on other projects..

like image 701
Ben Durkin Avatar asked Jul 29 '15 12:07

Ben Durkin


People also ask

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript. The regular expression in JavaScript is used to find the pattern in a string. So we are going to find the “dd-mmm-yyyy” pattern in the string using match() method.


1 Answers

The format() function is not standard for Date objects in javascript.

You have most likely seen this in an application running a date formatting library such as moment.js.

http://momentjs.com/

moment().format('MMMM Do YYYY, h:mm:ss a');

Your example could be rewrote in moment.js like this:

var timestamp = moment().format("DD/MM/YYYY HH:mm:ss") + " (" + GetLoggedUserName() + ")";
like image 133
Curtis Avatar answered Oct 03 '22 08:10

Curtis