可能重复:
从javascript中的日期减去天数
我有一个JavaScript基本上返回2天前的日期.它如下:
var x;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var twoDaysAgo = d.getDate()-2; //change day here
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var x = twoDaysAgo + "-" + m_names[curr_month] + "-" + curr_year;
document.write(x);
Run Code Online (Sandbox Code Playgroud)
假设今天是2012年12月12日,以上将返回2012年12月10日的日期.我不认为这会在我们进入新的月份时动态起作用,或者,将日期从-2更改为-15.它只能从本月3日开始工作.
我怎么能修改这个,所以当它是2012年12月12日今天,我希望它能在15天前将它更新给我,它应该是2012年11月27日......而不是2012年12月3日?
任何帮助赞赏.谢谢!我是一个Javascript新手.
我想通过从JavaScript中的特定日期减去X天数来查找日期.我的JavaScript函数接受2个参数.一个是日期值,另一个是需要减去的天数.
例如,我将我的参数日期传递给2009年7月27日,我将我的另一个参数传递给3.所以我想计算2009年7月27日前3天的日期.因此我们得到的结果日期是2009年7月24日.这可能在JavaScript中.谢谢你的帮助.
在新的Date()的帮助下我如何实现这一目标.我的代码:
var temp =new Date("October 13, 2014 22:34:17");
console.log(new Date(temp-1));
Run Code Online (Sandbox Code Playgroud)
需求:
before: Aug 4, 2014 11:59pm (EDT)
after: Aug 3, 2014 11:59pm (EDT)
Run Code Online (Sandbox Code Playgroud) 我从日期中减去天数的代码无法获得正确的结果.
function get_Ash_Wednesday_date (year, Easter_month, Easter_day) {
var Easter_date = new Date();
Easter_date.setFullYear(year, Easter_month-1, Easter_day);
var Ash_Wednesday = new Date();
Ash_Wednesday.setFullYear(year);
Ash_Wednesday.setDate(Easter_date.getDate()-46);
return Ash_Wednesday;
}
Run Code Online (Sandbox Code Playgroud)
从复活节开始,我减去46天.这通常可以追溯到二月,必须考虑到闰年.计算的灰烬周三日期为一天或两天,例如2012年灰烬周三应为02/22,2013应为02/13,2014应为03/05,2015应为02/18,2016应该是02/10.这个月每次都错了,应该是2月或3月.什么可以解释返回的日子和月份的差异?
Year: 2012 Ash Wednesday month 11 Ash Wednesday day 23 Easter Month: 4 Easter Day: 8
Year: 2013 Ash Wednesday month 12 Ash Wednesday day 16 Easter Month: 3 Easter Day: 31
Year: 2014 Ash Wednesday month 12 Ash Wednesday day 5 Easter Month: 4 Easter Day: 20
Year: 2015 Ash Wednesday month 11 …
Run Code Online (Sandbox Code Playgroud) 我在节点后端使用日期格式包,我今天可以使用
var today = dateFormat(new Date());
Run Code Online (Sandbox Code Playgroud)
我想以昨天约会的相同或其他方式.我仍然没有得到任何正确的方法.我暂时用大量代码手动计算昨天的日期.除了手动编写之外还有其他方法吗?
我正在尝试填充两个日期输入字段,一个包含今天的日期,另一个包含 30 天前(上个月)的日期。
我的控制台出现错误:priordate.getDate is not a function
这是我的代码,不确定我做错了什么:
//today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0, so always add + 1
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd};
if(mm<10){mm='0'+mm};
today = yyyy+'-'+mm+'-'+dd;
//30 days ago
var beforedate = new Date();
var priordate = new Date().setDate(beforedate.getDate()-30);
var dd2 = priordate.getDate();
var mm2 = priordate.getMonth()+1;//January is 0, so always add + 1
var yyyy2 = priordate.getFullYear();
if(dd2<10){dd2='0'+dd2};
if(mm2<10){mm2='0'+mm2};
var datefrommonthago = yyyy2+'-'+mm2+'-'+dd2;
// …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数来计算给定日期从今天开始的天数.(例如yesterday = 1
,last week = 7
,today = 0
,tomorrow = -1
等等)
看似简单,并使用Date()
我最初编写的JavaScript 函数:
let historicalDate = new Date(2017,05,17).getTime(); // example date: last week
let diff = Math.round((new Date().getTime() - historicalDate) / (24*60*60*1000) );
Run Code Online (Sandbox Code Playgroud)
在得到一些奇怪的结果后,我加密了代码,但仍然遇到了同样的问题,如下所示:
/**
* Returns an integer, representing the number of days since a given date
**/
function getNumDaysFromDate(historicalDate){
const day = 24*60*60*1000; // The number of milliseconds in one day
const now = new Date().getTime(); // The time …
Run Code Online (Sandbox Code Playgroud)