Skip to content

常用日期格式举例

  1. - 分隔符
分隔符是否补零yyyy Or yy格式举例
-✔️yyyyyyyy-MM-dd1/1/2012
-✔️yyyy-MM-dd99-01-01
-yyyyyyyy-M-d1/1/2012
-yyyy-M-d99-1-1
  1. / 分隔符
分隔符是否补零yyyy Or yy格式举例
/ or ,✔️yyyyyyyy/MM/dd1/1/2012
/ or ,✔️yyyy/MM/dd99/01/01
/ or ,yyyyyyyy/M/d1/1/2012
/ or ,yyyy/M/d99/1/1
  1. 无分隔符
分隔符是否补零yyyy Or yy格式举例
✔️yyyyyyyyMMdd20120101
✔️yyyyMMdd990101
yyyyyyyyMd201112
yyyyMd1112

JS内置对象 Date 对日期格式的支持情况

格式举例是否可用支持Date()不支持原因
yyyy-MM-dd1/1/2012✔️✔️
yy-MM-dd99-01-01✔️不支持yy
yyyy-M-d1/1/2012✔️✔️
yy-M-d99-1-1✔️不支持yy
yyyy/MM/dd1/1/2012✔️✔️
yy/MM/dd99/01/01✔️不支持yy
yyyy/M/d1/1/2012✔️✔️
yy/M/d99/1/1✔️不支持yy
yyyyMMdd20120101✔️不支持无分隔符
yyMMdd990101✔️不支持yy
yyyyMd201112不支持无分隔符
yyMd1112不支持yy

关于 new Date()

  • 参数可以为整数; 也可以为字符串; 但格式必须正确
  • 2020-12-01 与 2020-12-1 得到的结果就不一样
  • input date获取到的值时 Y-m-d 这种格式, new Date() 不支持
  • 可以补零,也可以不补零
  • Date中可设定的年份最小为1900
  • 只支持yyyy
  • Safari浏览器中,new Date()不支持 YYYY-MM-DD !
  • Date 对象支持的日期分隔符:
  1. 斜杠 ( / )
  2. 点号 ( . )
  3. 反斜杠 ( \ )
  4. 空格 ( )
  5. 逗号 ( , )
  6. 下划线 (_ )
  7. 连字符 ( - )
  • Date 对象支持的时间分隔符:
  1. 冒号 ( : )
  2. 点号 ( . )
  3. 分号 ( ; )
  4. 反斜杠 ( \ )
  5. 斜杠 ( / )

1. 获得X年之前的今天

一般用于Vue项目中element的日期选择器。

  • 需求:想要提交的日期区间有一个范围,当清空element的date-picker时触发。往往用于配合表单的提交。

得到的日期的格式为 yyyy-MM-dd

js
const cc = new Date().getTime()
var halfYear = 365 / 2 * 24 * 3600 * 1000;
var pastResult = cc - halfYear
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
console.log(pastYear + '-' + pastMonth + '-' + pastDay)
//打印格式为 XXXX-XX-XX
const cc = new Date().getTime()
var halfYear = 365 / 2 * 24 * 3600 * 1000;
var pastResult = cc - halfYear
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
console.log(pastYear + '-' + pastMonth + '-' + pastDay)
//打印格式为 XXXX-XX-XX
js
const cc = new Date().getTime()
var oneYear = 365 / 24 * 3600 * 1000; //将halfYear换为oneYear
var pastResult = cc - oneYear 
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
console.log(pastYear + '-' + pastMonth + '-' + pastDay)
//如果现在是2022年2月9日,则输出2021-02-09
const cc = new Date().getTime()
var oneYear = 365 / 24 * 3600 * 1000; //将halfYear换为oneYear
var pastResult = cc - oneYear 
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
console.log(pastYear + '-' + pastMonth + '-' + pastDay)
//如果现在是2022年2月9日,则输出2021-02-09
js
const cc = new Date().getTime()
var oneYear = 365 / 24 * 3600 * 1000; //将halfYear换为oneYear
var pastResult = cc - oneYear 
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
this.dateStart = pastYear + '-' + pastMonth + '-' + pastDay
this.dateEnd = pastYear + 1 + '-' + pastMonth + '-' + pastDay
const cc = new Date().getTime()
var oneYear = 365 / 24 * 3600 * 1000; //将halfYear换为oneYear
var pastResult = cc - oneYear 
var pastDate = new Date(pastResult),
  pastYear = pastDate.getFullYear(),
  pastMonth = pastDate.getMonth() + 1,
  pastDay = pastDate.getDate();
this.dateStart = pastYear + '-' + pastMonth + '-' + pastDay
this.dateEnd = pastYear + 1 + '-' + pastMonth + '-' + pastDay

2. 根据月份获得当月有多少天

js
function getMonthLength(date) {
    let d = new Date(date)
    //将日期设置为下月一号
    d.setMonth(d.getMonth()+1)
    d.setDate('1')
    //获得本月最后一天
    d.setDate(d.getDate()-1)
    return d.getDate()
}
getMonthLength("2022-01-02") //31
function getMonthLength(date) {
    let d = new Date(date)
    //将日期设置为下月一号
    d.setMonth(d.getMonth()+1)
    d.setDate('1')
    //获得本月最后一天
    d.setDate(d.getDate()-1)
    return d.getDate()
}
getMonthLength("2022-01-02") //31
js
function getMonthLength(year,month){
    return new Date(year,month,0).getDate()
}
getMonthLength(2022,02) //28
function getMonthLength(year,month){
    return new Date(year,month,0).getDate()
}
getMonthLength(2022,02) //28

3. 比较日期先后

注意格式

js
function compareTime(startdate, enddate) {
	var date1 = startdate;
	var date2 = enddate;
	if (Date.parse(date1) < Date.parse(date2)) {
		return '<'
	} else if (Date.parse(date1) > Date.parse(date2)) {
		return '>'
	} else {
		return '='
	}
}
let time1 = '2022-12-13'
let time2 = '2022-11-11'
compareTime(time1, time2)
function compareTime(startdate, enddate) {
	var date1 = startdate;
	var date2 = enddate;
	if (Date.parse(date1) < Date.parse(date2)) {
		return '<'
	} else if (Date.parse(date1) > Date.parse(date2)) {
		return '>'
	} else {
		return '='
	}
}
let time1 = '2022-12-13'
let time2 = '2022-11-11'
compareTime(time1, time2)

4. yyyyMMdd 转换为 yyyy-MM-dd

js
function formatDate(dateString) {
  var year = dateString.slice(0,4)
  var month = dateString.slice(4,6)
  var day = dateString.slice(6,8)
  var formattedDate = year + '-' + month + '-' + day
  return formattedDate
}
function formatDate(dateString) {
  var year = dateString.slice(0,4)
  var month = dateString.slice(4,6)
  var day = dateString.slice(6,8)
  var formattedDate = year + '-' + month + '-' + day
  return formattedDate
}

5. 比较格式为 yyyyMMdd 的日期大小

可以使用Date对象来比较两个日期的先后大小。先将两个日期字符串转换为Date对象,然后使用日期对象的getTime()方法获取时间戳,最后比较时间戳的大小即可。

以下是一个示例代码:

javascript
function compareDate(dateStr1, dateStr2) {
  const date1 = new Date(dateStr1.slice(0, 4), Number(dateStr1.slice(4, 6)) - 1, dateStr1.slice(6));
  const date2 = new Date(dateStr2.slice(0, 4), Number(dateStr2.slice(4, 6)) - 1, dateStr2.slice(6));
  const time1 = date1.getTime();
  const time2 = date2.getTime();
  if (time1 > time2) {
    return 1;
  } else if (time1 < time2) {
    return -1;
  } else {
    return 0;
  }
}
function compareDate(dateStr1, dateStr2) {
  const date1 = new Date(dateStr1.slice(0, 4), Number(dateStr1.slice(4, 6)) - 1, dateStr1.slice(6));
  const date2 = new Date(dateStr2.slice(0, 4), Number(dateStr2.slice(4, 6)) - 1, dateStr2.slice(6));
  const time1 = date1.getTime();
  const time2 = date2.getTime();
  if (time1 > time2) {
    return 1;
  } else if (time1 < time2) {
    return -1;
  } else {
    return 0;
  }
}

以上代码中,compareDate函数接受两个字符串形式的日期,将其转换为Date对象后比较大小。如果第一个日期比第二个日期晚,返回1,如果第一个日期比第二个日期早,返回-1,否则返回0。