如何在当前日期之前30天获得? [英] How to get 30 days prior to current date?

查看:69
本文介绍了如何在当前日期之前30天获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开始日历输入框和一个结束日历输入框.我们希望将当前日期之前30天的默认开始日历输入框和当前日期作为结束日历输入框的默认值.这是我的约会变量.

I have a start calendar input box and an end calendar input box. We want defaults start calendar input box 30 days prior to current date and the end calendar input box to be the current date. Here is my date vars.

var today = new Date(),
    dd    = today.getDate(),
    mm    = today.getMonth(),
    yyyy  = today.getFullYear(),
    month = ["January", "February", "March",
        "April", "May", "June", "July", "August",
        "September", "October" "November", "December"],
    startdate = month[mm] + ", " + yyyy.toString();

结束日期类似于var enddate = startdate - 30;,显然这是行不通的.

The end date would be something like var enddate = startdate - 30; Obviously this won't work.

因此,如果当前日期为2011年12月30日,我希望开始日期为2011年12月1日.

So if the current date is December 30, 2011 I'd want the start date to read December 1, 2011.

我的问题得到了回答……有点. Date.today();Date.today().add(-30);工作,但是我需要January 13, 2012格式的日期.不是Fri Jan 13 2012 10:48:56 GMT -055 (EST).有帮助吗?

My question was answered... sort of. Date.today(); and Date.today().add(-30); work but I need the date in the format of January 13, 2012. Not Fri Jan 13 2012 10:48:56 GMT -055 (EST). Any help?

更多撰写本文时为2018年.只需使用 Moment.js .最好.

MORE As of this writing it's 2018. Just use Moment.js. It's the best.

推荐答案

尝试使用出色的 Datejs JavaScript日期库(不再保留原始文档,因此您可能会对此主动维护的fork 感兴趣):

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

另请参见出色的 Moment.js JavaScript日期库:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

这篇关于如何在当前日期之前30天获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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