当前位置: 首页 > 后端技术 > Node.js

NPM酷库:dateformat时间字符串格式

时间:2023-04-03 23:34:08 Node.js

NPM酷库,每天两分钟,了解一个流行的NPM库。JavaScript中Date内置对象的toString()方法可以将时间对象转换为字符串,但是转换后的格式是这样的:ThuFeb01201810:28:41GMT+0800(CST)我们将其显示在UI这样的时间字符串对于用户阅读或者布局需求来说是不可接受的。我们经常需要一个特定格式的字符串来表示时间:2018-02-01如果使用JS内置的Date对象方法,则需要使用getFullYear()getMonth()等函数拼凑结果。dateformat今天我们使用dateformat库轻松地将时间对象格式化为我们需要的任何格式:constdateFormat=require('dateformat');常量现在=新日期();dateFormat(now,'dddd,mmmmdS,yyyy,h:MM:ssTT');//2018年2月1日,星期四,10:28:41AMdateFormat(now,'yyyy-mm-dd');//2018-02-01dateFormat(now,'isoDate');//2018-02-01参考https://github.com/felixge/no...

最新推荐
猜你喜欢