日期月显示问题 [英] Date Month display problem

查看:78
本文介绍了日期月显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自去年8月以来,我一直在网页上使用javascript来

显示网站更新一个月只减去一个月,这已经非常成功,但是1月份正在显示未定义的2004年。消息非常感谢

Jim


<<< var m = new Array(13); var n =新日期()


m [1] ="网站更新1月;; m [2] ="网站更新2月" ;; m [3] ="网站更新

March"; m [4] =" Site updated April" ;; m [5] =" Site updated May; m [6] =" Site updated

June" ;;

m [7] =" 7月更新网站; m [8] =" 8月更新网站; m [9] ="网站更新

九月" ;; m [10] ="网站更新十月"; m [11] ="网站更新

十一月" ;; m [12] ="网站更新十二月"

document.write(m [n.getMonth()+ 0] +"" + n.getFullYear())>>>

解决方案

Jim写道:

我自去年8月以来一直在网页上使用javascript来显示网站更新 ;月只减去一个月,这已经非常成功,但是1月份正在显示未定义的2004。帮助非常感谢
Jim

<<< var m = new Array(13); var n = new Date()

m [1] ="网站更新1月;;



[snip]


数组从0开始,而不是1。只需要一个数组来容纳12个项目,并且你需要从0开始编号
。目前,你正试图写出

字符串在m [0 ],你没有分配任何东西。


-

Jim Dabell


"吉姆" < ji*@internet.com>写道:

自去年8月以来,我一直在网页上使用javascript来显示网站更新。一个月只减去一个月,这已经非常成功,


让用户认为你经常更新页面? :)

但是1月份显示的是未定义的2004消息。

<<< var m = new Array(13); var n = new Date()

m [1] ="网站更新1月" ;; m [2] =网站更新2月; m [3] =网站更新
3月; m [4] =网站更新4月; m [5] =网站更新了May; m [6] ="网站更新
6月;
m [7] =网站更新7月; m [8] =网站更新8月; m [ 9] =网站更新
九月; m [10] =网站更新十月; m [11] =网站更新
十一月; m [12] =网站12月更新了
document.write(m [n.getMonth()+ 0] +"" + n.getFullYear())>>>



getMonth返回月份编号,从1月份的0开始,到12月份以

11结尾。你的桌子有1 => 1月,所以它给了上个月。

这意味着你可以改变

m [12] =网站更新12月



m [0] ="网站更新12月>

它将在本月有效。来自getFullYear的那一年是当前的

一年,所以如果这就是你将获得2004年12月。

我推荐以下内容:

---

var monthName = [

1月,2月,3月,4月,5月, June,July,

August,September,October,November,Decembe r];

var now = new Date();

now.setDate(1);

now.setMonth(now.getMonth() - 1);

document.write(" Site updated",

monthName(now.getMonth()),

"",now.getFullYear( ));

---

我们将日期设置为一个月,所以getMonth和getFullYear

指的是同一天。将月份设置为-1将给出去年12月



我将日期设置为1,以避免在更改月份时出现问题。 />
否则,在3月31日将月份设置为1将

给出2月31日,这将标准化为给予第3个

3月。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


" Jim" < ji*@internet.com>在留言中写道

新闻:3f *********************** @ news.dial.pipex.com ...

自去年8月以来,我一直在网页上使用javascript来显示网站更新。月只减去一个月,这已经非常成功,但是1月份正在显示未定义的2004。帮助非常感谢
Jim

<<< var m = new Array(13); var n = new Date()

m [1] ="网站更新1月;; m [2] ="网站更新2月; m [3] ="网站
更新3月" ;; m [4] ="网站更新四月; m [5] ="网站更新五月; m [6] =网站
更新六月;
m [7] =网站更新七月; m [ 8] =网站更新8月; m [9] =网站更新
9月; m [10] =网站更新10月; m [11] =网站更新 11月" ;; m [12] ="网站更新12月"
document.write(m [n.getMonth()+ 0] +"" + n.getFullYear())>>> ;



相反怎么样?


< html>

< head>

< title> SiteUpdated.htm< / title>

< script language =" javascript" type =" text / javascript">

<! -

var m = new Array(11);

m [0 ] =" 1月" ;;

m [1] =" February";

m [2] =" March";

m [ 3] =" April" ;;

m [4] =" May";

m [5] =" June";

m [6] =" July" ;;

m [7] =" August";

m [8] =" September";

m [9] =" October" ;;

m [10] =" November" ;;

m [11] =" December"

var n = new Date();

document.write(" Site updated" + m [n.getMonth()] +"" + n.getFullYear()); < br $>
// - >

< / script>

< / head>

< body> ;

< / body>

< / html>


I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated
Jim

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>

解决方案

Jim wrote:

I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated
Jim

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";


[snip]

Arrays start from 0, not 1. You only need an array to hold 12 items, and
you need to number them from 0. At the moment, you are trying to write out
the string in m[0], which you haven''t assigned to anything.

--
Jim Dabell


"Jim" <ji*@internet.com> writes:

I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful,
In making the users think you update the page frequently? :)
but January is showing a "undefined 2004" message.

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>



getMonth returns the month number starting with 0 for January and ending with
11 for December. Your table has 1=>January, so it gives the previous month.
That means that you can change
m[12]="Site updated December"
to
m[0]="Site updated December"
and it will work for the month. The year from getFullYear is the current
one, so you will get December 2004 if that is all you do.
I recommend the following instead:
---
var monthName = [
"January","February","March","April","May","June", "July",
"August","September","October","November","Decembe r"];
var now = new Date();
now.setDate(1);
now.setMonth(now.getMonth()-1);
document.write("Site updated ",
monthName(now.getMonth()),
" ",now.getFullYear());
---
We set the date back by one month, so the getMonth and getFullYear
refer to the same date. Setting the month to -1 will give December
of the previous year.

I set the date to the 1st to avoid problems when changing month.
Otherwise, setting the month back by one on the 31th of March would
give the 31th of February, which is normalized to give the 3rd of
March.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


"Jim" <ji*@internet.com> wrote in message
news:3f***********************@news.dial.pipex.com ...

I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated
Jim

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>


How about this instead?

<html>
<head>
<title>SiteUpdated.htm</title>
<script language="javascript" type="text/javascript">
<!--
var m = new Array(11);
m[0] = "January";
m[1] = "February";
m[2] = "March";
m[3] = "April";
m[4] = "May";
m[5] = "June";
m[6] = "July";
m[7] = "August";
m[8] = "September";
m[9] = "October";
m[10] = "November";
m[11] = "December"
var n = new Date();
document.write("Site updated " + m[n.getMonth()] + " " + n.getFullYear());
// -->
</script>
</head>
<body>
</body>
</html>


这篇关于日期月显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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