本文介绍了今天的日期 - 使用JavaScript的30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要今天的日期-30天,但格式为:2016-06-08
I need to get today's date -30 days but in the format of: "2016-06-08"
我试过 setDate (date.getDate() - 30);
-30天。
我试过 date.toISOString( ).split('T')[0]
格式。
两者都有效,但不知何故不能一起使用。
Both work, but somehow cannot be used together.
推荐答案
不返回 Date
对象,它返回自1970年1月1日00:00:00 UTC以来的毫秒数。您需要单独调用:
setDate()
doesn't return a Date
object, it returns the number of milliseconds since 1 January 1970 00:00:00 UTC. You need separate calls:
var date = new Date();
date.setDate(date.getDate() - 30);
var dateString = date.toISOString().split('T')[0]; // "2016-06-08"
这篇关于今天的日期 - 使用JavaScript的30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!