posts - 1,  comments - 6,  trackbacks - 0
在JS中获取系统当前时间是用到Date这个对象,在实际应用时,常常会用到不同的显示格式,下面是一些处理方法!
  var now = new Date();
  strTime = now.toLocaleString();
  strYear = now.getYear();
  strMonth = now.getMonth();
  strDay = now.getDay();
   如果要将时间显示为2007/02/12这种格式,人们往往会采用如下的方式:
   displayTime = strYear +"/"+strMonth+"/"+strDay;
   也可以加入一些判断,以满足要求,但在实际中这种办法是做不到的,显示的格式与我们期望的要求,有很大的差别,为了可以满足要求我们做如下的处理,
   if(strTime.substring(5,6)<10){
 var displayTime = strTime.substring(0,4)+"/0"+strTime.substring(5,6)+"/"+strTime.substring(7,9);
 }else{
 var displayTime = strTime.substring(0,4)+"/"+strTime.substring(5,7)+"/"+strTime.substring(8,10);
 }
这样做虽然有些麻烦,但具有很强的灵活性。
posted on 2007-02-12 14:21 Andy zhang 阅读(9161) 评论(6)  编辑  收藏


FeedBack:
# re: 关于JS获取系统当前时间
2007-04-27 17:17 | dfsa
fdsaf  回复  更多评论
  
# re: 关于JS获取系统当前时间
2008-02-03 10:09 | 1
你 说的不对 strDay = now.getDay(); 是对应日期的星期几而不是你说的2007/02/12  回复  更多评论
  
# re: 关于JS获取系统当前时间
2008-07-30 16:27 | rzzfch520
if(strTime.substring(5,6)<10){
var displayTime = strTime.substring(0,4)+"/0"+strTime.substring(5,6)+"/"+strTime.substring(7,9);
}else{
var displayTime = strTime.substring(0,4)+"/"+strTime.substring(5,7)+"/"+strTime.substring(8,10);
}

这样做是不可能实现的if(strTime.substring(5,6)<10)这一句就有问题,应该是
if(strTime.substring(5,6) > 0)
然后还需要同样的同日进行判断  回复  更多评论
  
# re: 关于JS获取系统当前时间
2008-09-05 09:16 | zf
恩 这段代码有问题  回复  更多评论
  
# re: 关于JS获取系统当前时间[未登录]
2008-10-06 22:28 | dd
@1
就是  回复  更多评论
  
# re: 关于JS获取系统当前时间
2008-12-30 13:24 | 北京时间
你说的不对 strDay = now.getDay(); 是对应日期的星期几而不是你说的2007/02/12   回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论