最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get next date and day - Stack Overflow

programmeradmin0浏览0评论

I have a table to which i dynamically add rows. The number of days is equal to the date difference of the dates inserted by user. On the dynamic rows i want to add three fields. The first two are date and day filed. For that I need to know the next date and the corresponding day. For example user enters 10-2-2012. I need to insert The next dates like 17-2-2012,18-2-2012... and corresponding days like Wednesday, Thursday..etc I have used the following function to get next date

function getTomorrow(d,offset)
{
    if (!offset)
    {
       offset = 1
    }
    return new Date(new Date().setDate(d.getDate() + offset));
}

But it shows error 16-2-2012 has no getDate() function. Am not able to find next date and the corresponding day. Is there any way to get it?

I have a table to which i dynamically add rows. The number of days is equal to the date difference of the dates inserted by user. On the dynamic rows i want to add three fields. The first two are date and day filed. For that I need to know the next date and the corresponding day. For example user enters 10-2-2012. I need to insert The next dates like 17-2-2012,18-2-2012... and corresponding days like Wednesday, Thursday..etc I have used the following function to get next date

function getTomorrow(d,offset)
{
    if (!offset)
    {
       offset = 1
    }
    return new Date(new Date().setDate(d.getDate() + offset));
}

But it shows error 16-2-2012 has no getDate() function. Am not able to find next date and the corresponding day. Is there any way to get it?

Share Improve this question edited Feb 16, 2012 at 8:46 Sunil Kumar B M 2,7951 gold badge27 silver badges32 bronze badges asked Feb 16, 2012 at 8:33 user1199657user1199657 3
  • 1 Please provide a demonstration of your problem. What is d? How do you call getTomorrow? – Zeta Commented Feb 16, 2012 at 8:36
  • d is the date in dd-mm-yy format – user1199657 Commented Feb 16, 2012 at 8:38
  • In case you find yourself doing a lot of date manipulation in the future, I can't remend moment.js enough. Adding days to an existing date is just a matter of moment().add('d', 1);. – geca Commented Feb 16, 2012 at 8:52
Add a ment  | 

3 Answers 3

Reset to default 4

You have to convert the string d into a Date object:

function getTomorrow(d,offset){
    if (!offset){
        offset = 1;
    }
    if(typeof(d) === "string"){
        var t = d.split("-"); /* splits dd-mm-year */
        d = new Date(t[2],t[1] - 1, t[0]);
    //  d = new Date(t[2],t[1] - 1, t[0] + 2000); /* for dd-mm-yy */
    }
    return new Date(d.setDate(d.getDate() + offset));
}    

document.write(getTomorrow('16-02-2012',20) + ' test');

var k = getTomorrow('16-02-2012',1);
var myTime = k.getDate()+'-'+(k.getMonth()+1)+'-'+k.getFullYear();

alert(myTime);

JSFiddle Demo. See also http://www.w3schools./jsref/jsref_obj_date.asp.

 var d=new Date();                 

d.setTime((d.getTime() + 86400 * 1000*1)); 

document.write(d);

document.write(d.getDay()+"-"+parseInt(d.getMonth()+1)+"-"+d.getFullYear());

if you need to know the date of day after tommorow , just change 1000*1 to 1000*2.

i giving a example

var year = 2010, month = 9, day = 14;
    // The value of `meses`
    var offset = 1; // Tomorow

    var future_date = new Date(year, month , day+offset);

    console.log(future_date);
发布评论

评论列表(0)

  1. 暂无评论