最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript utility for date formatting - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

This 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");
发布评论

评论列表(0)

  1. 暂无评论