js根据时间戳获取格式化日期

痛定思痛。 2022-06-09 10:56 417阅读 0赞

js里面没有项php里面的date函数,可以直接获取格式化的日期。需要自己根据date对象分别去获取然后格式化。

  1. function getFormatDate(timestamp) {
  2. timestamp = parseInt(timestamp + '000');
  3. var newDate = new Date(timestamp);
  4. Date.prototype.format = function (format) {
  5. var date = {
  6. 'M+': this.getMonth() + 1,
  7. 'd+': this.getDate(),
  8. 'h+': this.getHours(),
  9. 'm+': this.getMinutes(),
  10. 's+': this.getSeconds(),
  11. 'q+': Math.floor((this.getMonth() + 3) / 3),
  12. 'S+': this.getMilliseconds()
  13. };
  14. if (/(y+)/i.test(format)) {
  15. format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  16. }
  17. for (var k in date) {
  18. if (new RegExp('(' + k + ')').test(format)) {
  19. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  20. ? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
  21. }
  22. }
  23. return format;
  24. }
  25. return newDate.format('yyyy-MM-dd h:m');
  26. }

示例:

Center

发表评论

表情:
评论列表 (有 0 条评论,417人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JS获取时间

    此文章来自转载 : 链接: [JS获取本周、本季度、本月、上月、本年的开始日期、结束日期][JS]. / 获取本周、本季度、本月、上月的开始日期、结束日期 /