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

Get month name from Date using JavaScript but so that the date is 15 days ahead of actual date - Stack Overflow

programmeradmin12浏览0评论

How can i generate the Month name (e.g: Oct/October) in JavaScript but also manipulate it so that it is 15 days ahead of the actual date?

I have found 2 nice scripts but cannot combine the 2 together.

<html>
<head>
<title>Combine Date Values</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--

var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

var current_date = new Date();
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();

document.write("The current date is " + months[month_value] + " " +
day_value + ", " + year_value);

//-->
</script>
</body>
</html>

^^^^ the above will display the current date ^^^^

<script type="text/javascript"> 
Date.prototype.addDays = function(days) {
this.setDate(this.getDate()+days);
}

var d = new Date();
d.addDays(15);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(curr_month + "/" + curr_date + "/" + curr_year);
</script>

^^^^ this will now display a date which is 15 days ahead^^^^

So the end result should be something like this. EXAMPLE 1 Actual Date: 28 MAY 2013 Displayed Date: 12 JUNE 2013

EXAMPLE 2 Actual Date: 15 MAY 2013 Displayed Date: 30 MAY 2013

EXAMPLE 3 Actual Date: 16 MAY 2013 Displayed Date: 01 JUNE 2013

How can i generate the Month name (e.g: Oct/October) in JavaScript but also manipulate it so that it is 15 days ahead of the actual date?

I have found 2 nice scripts but cannot combine the 2 together.

<html>
<head>
<title>Combine Date Values</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--

var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

var current_date = new Date();
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();

document.write("The current date is " + months[month_value] + " " +
day_value + ", " + year_value);

//-->
</script>
</body>
</html>

^^^^ the above will display the current date ^^^^

<script type="text/javascript"> 
Date.prototype.addDays = function(days) {
this.setDate(this.getDate()+days);
}

var d = new Date();
d.addDays(15);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(curr_month + "/" + curr_date + "/" + curr_year);
</script>

^^^^ this will now display a date which is 15 days ahead^^^^

So the end result should be something like this. EXAMPLE 1 Actual Date: 28 MAY 2013 Displayed Date: 12 JUNE 2013

EXAMPLE 2 Actual Date: 15 MAY 2013 Displayed Date: 30 MAY 2013

EXAMPLE 3 Actual Date: 16 MAY 2013 Displayed Date: 01 JUNE 2013

Share Improve this question asked May 28, 2013 at 14:58 user2429067user2429067 231 gold badge1 silver badge4 bronze badges 1
  • Where exactly are you stuck? You have to get the month value from d, so months[d.getMonth()]. There is nothing particularly complicated about it, unless you don't understand each script on its own. In that case you should read up on some JS basics again: eloquentjavascript.net. – Felix Kling Commented May 28, 2013 at 15:02
Add a comment  | 

5 Answers 5

Reset to default 8

Here is the sample you are looking for,

var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

var current_date = new Date();
current_date.setDate(current_date.getDate() + 15);
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();

document.write("The current date is " + months[month_value] + " " + day_value + ", " + year_value);

http://jsfiddle.net/UXy8V/1/

See comments in code for what's going on.

var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; // Store month names in array
var d = new Date(); // Create date object with current date
d.setDate(d.getDate() + 15); // Add 15 days to date object
var month = months[d.getMonth()]; // Get month value (Jan = 0, Feb = 1, .etc) then get month string from array

The toDateString() function returns date in a format, of which we can get the short month name (Jun, Aug, Sep ...) by using substr().

var date_obj = new Date();
date_obj.toDateString().substr(4, 3);
var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];

// d = your date
// a = your additional days

function dateFormat1(d, a){
  var t = new Date(d);
  t.setDate(t.getDate()+a);
  return t.getDate()+' '+monthNames[t.getMonth()]+', '+t.getFullYear();
}

function dateFormat2(d, a){
  var t = new Date(d);
  t.setDate(t.getDate()+a);
  return t.getDate()+' '+monthShortNames[t.getMonth()]+', '+t.getFullYear();
}

Simple answer:

    const date = new Date();
    const monthName = date.toLocaleString(
              undefined,
              { month: 'long' }
            )

you can define your locale instead of using undefined

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论