简体   繁体   English

如何在Javascript中将日期向前滚动到指定的日期?

[英]How do I roll a date forward to a specified day in Javascript?

I'm working on a form that needs to automatically calculate the day the form is being submitted in the format ('MMDDYYYY'), and then on the click of one of two links a link, calculate the closest first day of the coming month, and the closest 15th day of the coming month. 我正在处理一个需要自动计算格式('MMDDYYYY')格式的表单的表单,然后单击两个链接之一的链接,计算下个月的最近的第一天,以及下个月最近的第15天。

I already created a script that pulls in the date and outputs it to a variable in the format I need, but I need help in calculating the roll forward. 我已经创建了一个脚本,该脚本提取日期并将其输出为所需格式的变量,但是在计算前滚时需要帮助。

Here's an example of the logic I'm thinking I need: 这是我需要的逻辑示例:

If the current date is 04092013, on a the button press labeled "Coming 1st of Month" a variable value of 05012013 would be calculated. 如果当前日期是04092013,则在按钮上标记为“即将到来的第一个月”的按钮将被计算为05012013的变量值。

If the current date is 04092013, on a button press labeled "Coming 15th of Month" a variable value of 04152013 would be calculated. 如果当前日期是04092013,则在按钮上标记为“即将到来的15月”,将计算出变量值04152013。

If the current date is 04162013 or any date up to the end of the current month, on a button press labeled "Coming 15th of Month" a variable value of 05152013 would be calculated. 如果当前日期是04162013或直到当前月末的任何日期,则在按钮上标记为“即将到来的15日”,将计算出变量值05152013。

Look at the Date object, it should provide what you need: 查看Date对象,它应提供您所需的内容:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Date

For example something like this for your first 2 buttons: 例如,您前两个按钮的内容如下:

$(function() {
    var date = new Date(2013, 10, 24);

    $("#date").html(date.toString());
    $("#date1").html((new Date(date.getYear(), date.getMonth()+1, 1)).toString());
    $("#date2").html((new Date(date.getYear(), date.getMonth()+1, 15)).toString());
});

(Though I'm sure there are easier ways to do this if you look through the Date documenation) (尽管我敢肯定,如果您查看“日期”文档,会有更简便的方法来执行此操作)

Here is an example. 这是一个例子。 If you want it back as a string, you'll have to format it at the end as desired (work with UTC ). 如果您希望将其作为字符串返回,则必须在最后将其格式化(使用UTC )。

If you put in a date that has the same day number as you're asking of it, it returns the same day, not moving forwards a month. 如果您输入的日期与您要求的日期具有相同的天数,则该日期将返回同一天,而不向前移动一个月。

var date_string = '04092013';
                // MMDDYYYY

function nextNthOfMonth(date_string, n) {
    var date;
    // n to Int, default 1
    n = (+n || 1);
    // date_string to ISO 8601
    date_string = // "yyyy-MM-ddTHH:mm:ssZ"
          date_string.slice(4)
        + '-' + date_string.slice(0, 2)
        + '-' + date_string.slice(2, 4)
        + 'T00:00:00Z';
    // construct date object
    date = new Date(date_string);
    // fix to desired date
    if (n < date.getUTCDate()) { // adjust for month if req.
        date.setUTCMonth(date.getUTCMonth() + 1);
    }
    date.setUTCDate(n);
    return date; // or format as desired
}

nextNthOfMonth(date_string, 1);
// Wed May 01 2013 01:00:00 GMT+0100 (GMT Daylight Time)
nextNthOfMonth(date_string, 15);
// Mon Apr 15 2013 01:00:00 GMT+0100 (GMT Daylight Time)

Try 尝试

function getDate() {
    var s = document.getElementById('date').value;
    if(s){
        return new Date(s.substring(4), parseInt(s.substring(2, 4), 10) - 1, s.substring(0, 2))
    }
}

function comingDate(date, day){
    date = new Date(date);
    var cday = date.getDate();
    date.setDate(day);
    if(cday >= day){
        date.setMonth(date.getMonth() + 1)
    }
    return date
}

function f15(){
    var d = getDate();
    var next = comingDate(d, 15);
    console.log(next)
}

function f1(){
    var d = getDate();
    var next = comingDate(d, 1);
    console.log(next)
}

Demo: Fiddle 演示: 小提琴

Since you know the format of your date, split the input into parts: 由于您知道日期的格式,因此将输入分成几部分:

var dateStr = '04092013'
var month = dateStr.substr(0,2);
var day = dateStr.substr(2,2);
var year = dateStr.substr(4,4);

The construct a new date base on the rule you want to set: 根据您要设置的规则构造一个新的日期:

var newDate;
switch(rule)
{
  case 'rule1':
  newDate = new Date(year, month, 1);//first month is 0
  break;
  case 'rule2':
  newDate = new Date(year, month, 15);//first month is 0
  break;
}
  • remember to check if the day is greater that 15. 记得检查一天是否大于15。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM