使用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");
我怎么做呢?
当前回答
UPDATE 2021-10-06: Added Day.js and remove spurious edit by @ashleedawg UPDATE 2021-04-07: Luxon added by @Tampa. UPDATE 2021-02-28: It should now be noted that Moment.js is no longer being actively developed. It won't disappear in a hurry because it is embedded in so many other things. The website has some recommendations for alternatives and an explanation of why. UPDATE 2017-03-29: Added date-fns, some notes on Moment and Datejs UPDATE 2016-09-14: Added SugarJS which seems to have some excellent date/time functions.
好吧,既然还没有人给出真正的答案,那我的答案就是。
库当然是以标准方式处理日期和时间的最佳选择。在日期/时间计算中有许多边缘情况,因此能够将开发移交给库是非常有用的。
以下是主要Node兼容的时间格式化库列表:
Day.js [added 2021-10-06] "Fast 2kB alternative to Moment.js with the same modern API" Luxon [added 2017-03-29, thanks to Tampa] "A powerful, modern, and friendly wrapper for JavaScript dates and times." - MomentJS rebuilt from the ground up with immutable types, chaining and much more. Moment.js [thanks to Mustafa] "A lightweight (4.3k) javascript date library for parsing, manipulating, and formatting dates" - Includes internationalization, calculations and relative date formats - Update 2017-03-29: Not quite so light-weight any more but still the most comprehensive solution, especially if you need timezone support. - Update 2021-02-28: No longer in active development. date-fns [added 2017-03-29, thanks to Fractalf] Small, fast, works with standard JS date objects. Great alternative to Moment if you don't need timezone support. SugarJS - A general helper library adding much needed features to JavaScripts built-in object types. Includes some excellent looking date/time capabilities. strftime - Just what it says, nice and simple dateutil - This is the one I used to use before MomentJS node-formatdate TimeTraveller - "Time Traveller provides a set of utility methods to deal with dates. From adding and subtracting, to formatting. Time Traveller only extends date objects that it creates, without polluting the global namespace." Tempus [thanks to Dan D] - UPDATE: this can also be used with Node and deployed with npm, see the docs
还有一些非node库:
Datejs[感谢Peter Olson] -没有打包在npm或GitHub中,所以不太容易与Node一起使用-不太推荐,因为自2007年以来没有更新!
其他回答
这是我写的一个轻量级的简单日期格式库,可以在node.js和浏览器上运行
安装
使用NPM安装
npm install @riversun/simple-date-format
or
直接加载(浏览器),
<script src="https://cdn.jsdelivr.net/npm/@riversun/simple-date-format/lib/simple-date-format.js"></script>
加载库
ES6
import SimpleDateFormat from "@riversun/simple-date-format";
CommonJS node.js)
const SimpleDateFormat = require('@riversun/simple-date-format');
Usage1
const date = new Date('2018/07/17 12:08:56');
const sdf = new SimpleDateFormat();
console.log(sdf.formatWith("yyyy-MM-dd'T'HH:mm:ssXXX", date));//to be "2018-07-17T12:08:56+09:00"
用钢笔跑
Usage2
const date = new Date('2018/07/17 12:08:56');
const sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
console.log(sdf.format(date));//to be "2018-07-17T12:08:56+09:00"
格式化模式
https://github.com/riversun/simple-date-format#pattern-of-the-date
替代# 6233…
将UTC偏移量添加到本地时间,然后使用Date对象的toLocaleDateString()方法将其转换为所需的格式:
// Using the current date/time
let now_local = new Date();
let now_utc = new Date();
// Adding the UTC offset to create the UTC date/time
now_utc.setMinutes(now_utc.getMinutes() + now_utc.getTimezoneOffset())
// Specify the format you want
let date_format = {};
date_format.year = 'numeric';
date_format.month = 'numeric';
date_format.day = '2-digit';
date_format.hour = 'numeric';
date_format.minute = 'numeric';
date_format.second = 'numeric';
// Printing the date/time in UTC then local format
console.log('Date in UTC: ', now_utc.toLocaleDateString('us-EN', date_format));
console.log('Date in LOC: ', now_local.toLocaleDateString('us-EN', date_format));
我正在创建一个默认为本地时间的日期对象。我添加了UTC偏移量。我正在创建一个日期格式化对象。我正在以所需的格式显示UTC日期/时间:
这里有一个方便的香草单行(改编自此):
Var timestamp = new Date((dt = new Date()).getTime() - dt. gettimezoneoffset () * 60000) .toISOString () .replace (/ (. *), (. *) \ T . .* /, ' $ 1 $ 2 ') console.log(时间戳)
输出:2022-02-11 11:57:39
我需要一个简单的格式化库,不需要locale和语言支持。所以我修改了
http://www.mattkruse.com/javascript/date/date.js
并且使用它。参见https://github.com/adgang/atom-time/blob/master/lib/dateformat.js
文档非常清楚。
使用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'