I would like to use a utility function, which I can use as follows:
var d = new Date(); formatDate(d, "YYYY-mm-dd HH:MM:ss");
I know I can implement this function by myself but I would like to use something "standard". Besides I would like this function to run in both client side and server side (i.e. in browser and node.js
) and not to bring unnecessary dependencies. (for example, I would not like to use a jQuery
function)
There are a lot of Q&A about date formatting but I am still not sure if there is a utility
I would like to use a utility function, which I can use as follows:
var d = new Date(); formatDate(d, "YYYY-mm-dd HH:MM:ss");
I know I can implement this function by myself but I would like to use something "standard". Besides I would like this function to run in both client side and server side (i.e. in browser and node.js
) and not to bring unnecessary dependencies. (for example, I would not like to use a jQuery
function)
There are a lot of Q&A about date formatting but I am still not sure if there is a utility
Share Improve this question edited Jul 3, 2012 at 16:56 Michael asked Jul 3, 2012 at 16:47 MichaelMichael 10.3k12 gold badges63 silver badges112 bronze badges 3-
Why don't you just use the stuff that's already in the
Date
object? Also, you say you don't want unnecessary dependencies but you do want to use jQuery, the #1 unnecessary dependency ever? – Tom van der Woerdt Commented Jul 3, 2012 at 16:50 -
@TomvanderWoerdt Sorry. I meant I would not like to use
jQuery
. Edited the question. Thanks. – Michael Commented Jul 3, 2012 at 16:56 - What formats do you need? Only that one? – Simeon Commented Jul 3, 2012 at 17:01
3 Answers
Reset to default 3This lightweight library is what you need. Just remove unwanted parts of it.
http://momentjs./
Check out
Date.js
It should do what you're looking for
Use date.js with the folowing code:
var d = Date.today().set({ day: 15, hour: 8, minute: 30 });
d.toString("YYYY-mm-dd HH:MM:ss");