使用NodeJS,我想将Date格式化为以下字符串格式:
var ts_hms = new Date(UTC);
ts_hms.format("%Y-%m-%d %H:%M:%S");
我怎么做呢?
使用NodeJS,我想将Date格式化为以下字符串格式:
var ts_hms = new Date(UTC);
ts_hms.format("%Y-%m-%d %H:%M:%S");
我怎么做呢?
当前回答
使用x-date模块,它是x类库的子模块之一;
require('x-date') ;
//---
new Date().format('yyyy-mm-dd HH:MM:ss')
//'2016-07-17 18:12:37'
new Date().format('ddd , yyyy-mm-dd HH:MM:ss')
// 'Sun , 2016-07-17 18:12:51'
new Date().format('dddd , yyyy-mm-dd HH:MM:ss')
//'Sunday , 2016-07-17 18:12:58'
new Date().format('dddd ddSS of mmm , yy')
// 'Sunday 17thth +0300f Jul , 16'
new Date().format('dddd ddS mmm , yy')
//'Sunday 17th Jul , 16'
其他回答
总的来说,我并不反对图书馆。在这种情况下,通用库似乎有些多余,除非应用程序过程的其他部分过时得很严重。
编写这样的小实用函数对于初学者和熟练的程序员来说都是一个有用的练习,对于我们中的新手来说也是一个学习经验。
function dateFormat (date, fstr, utc) {
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
case '%m': m = 1 + date[utc + 'Month'] (); break;
case '%d': m = date[utc + 'Date'] (); break;
case '%H': m = date[utc + 'Hours'] (); break;
case '%M': m = date[utc + 'Minutes'] (); break;
case '%S': m = date[utc + 'Seconds'] (); break;
default: return m.slice (1); // unknown code, remove %
}
// add leading zero if required
return ('0' + m).slice (-2);
});
}
/* dateFormat (new Date (), "%Y-%m-%d %H:%M:%S", true) returns
"2012-05-18 05:37:21" */
现代网络浏览器(和Node.js)通过Intl对象公开国际化和时区支持,该对象提供了一个Intl. datetimeformat .prototype. formattoparts()方法。
您可以在没有添加库的情况下执行以下操作:
function format(dateObject){ let dtf = new Intl.DateTimeFormat("en-US", { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); var parts = dtf.formatToParts(dateObject); var fmtArr = ["year","month","day","hour","minute","second"]; var str = ""; for (var i = 0; i < fmtArr.length; i++) { if(i===1 || i===2){ str += "-"; } if(i===3){ str += " "; } if(i>=4){ str += ":"; } for (var ii = 0; ii < parts.length; ii++) { let type = parts[ii]["type"] let value = parts[ii]["value"] if(fmtArr[i]===type){ str = str += value; } } } return str; } console.log(format(Date.now()));
我在Nodejs和angularjs中使用dateformat,很好
安装
$ npm install dateformat
$ dateformat --help
demo
var dateFormat = require('dateformat');
var now = new Date();
// Basic usage
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
dateFormat(now, "isoDateTime");
// 2007-06-09T17:46:21
// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
dateFormat(now, "hammerTime");
// 17:46! Can't touch this!
// You can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007
...
在反映你的时区时,你可以使用这个
新日期() 新日期( 时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制。时间限制 ); var curr_time = dateString.toISOString()。代表(“T”,“”)。substr (0, 19); 游戏机。log (curr_time);
我认为这实际上回答了你的问题。 在javascript中处理日期/时间是很烦人的。 在我长了几根白头发之后,我发现这其实很简单。
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
var hours = date.getUTCHours();
var min = date.getUTCMinutes();
var sec = date.getUTCSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = ((hours + 11) % 12 + 1);//for 12 hour format
var str = month + "/" + day + "/" + year + " " + hours + ":" + min + ":" + sec + " " + ampm;
var now_utc = Date.UTC(str);
这里有一把小提琴