开发者

Use Javascript to Write a Day in the Future

I found this code online. I was trying to get it to write the date 9 days from today, but I can't figure out how to do that. Does anyone know what to change in this code?

var d_names = new Array("Sunday,", "Monday,", "Tuesday,",
"Wednesday,", "Thursday,", "Friday,", "Saturday,");

var m_names = new Array("January ", "February ", "March ", 
"April ", "May ", "June ", "July ", "August ", "September ", 
"October ", "November ", "December ");

var d = new Date();
var curr_day = d.getDay();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }
var curr_month = d.getMonth();
var curr_year = d.getFullYear();

document.write(d_names[curr_day] + " " + m_names[curr_month] + curr_date + "<SUP>" +     sup   + "</SUP> " + " " + curr_year);

This formats the 开发者_开发知识库date like: Saturday, August 20th 2011


Right after:

var d = new Date();

Write:

d.setDate(d.getDate()+9);

So you have:

var d = new Date();
d.setDate(d.getDate()+9);
var curr_day = d.getDay();
var curr_date = d.getDate();


I would use the "getTime" method of the date object with returns the number of milliseconds since Jan 1, 1970 and then add the number of milliseconds in 9 days.

var d = new Date();
var date9Days = new Date(d.getTime()+777600000);


var meh = new Date();
var daysAhead = 9;
meh.getTime() + 24 * 60 * 60 * 1000;
var newmeh = new Date(meh.getTime() + daysAhead * 24 * 60 * 60 * 1000);

That's how you get now + 9 days. Then just pass that date to your function


var d_names = new Array("Sunday,", "Monday,", "Tuesday,",
"Wednesday,", "Thursday,", "Friday,", "Saturday,");

var m_names = new Array("January ", "February ", "March ", 
"April ", "May ", "June ", "July ", "August ", "September ", 
"October ", "November ", "December ");

var d = new Date();
d.setDate(d.getDate()+9);
var curr_day = d.getDay();
var curr_date = d.getDate();
var sup = "";

if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }
var curr_month = d.getMonth();
var curr_year = d.getFullYear();

document.write(d_names[curr_day] + " " + m_names[curr_month] + curr_date + "<SUP>" +     sup   + "</SUP> " + " " + curr_year);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜