从JS日期对象中获取YYYYMMDD格式的字符串? [英] Get String in YYYYMMDD format from JS date object?

查看:540
本文介绍了从JS日期对象中获取YYYYMMDD格式的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JS将日期对象转换为 YYYYMMDD 格式的字符串。有没有比连接 Date.getYear() Date.getMonth() Date.getDay()

I'm trying to use JS to turn a date object into a string in YYYYMMDD format. Is there an easier way than concatenating Date.getYear(), Date.getMonth(), and Date.getDay()?

推荐答案

我经常使用的代码改变: p>

Altered piece of code I often use:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');
};

var date = new Date();
date.yyyymmdd();

这篇关于从JS日期对象中获取YYYYMMDD格式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆