Javascript:日期对象(Date)的基本用法

页面导航:首页 > 网络编程 > JavaScript > Javascript:日期对象(Date)的基本用法

Javascript:日期对象(Date)的基本用法

来源: 作者: 时间:2016-02-03 09:20 【

本文代码整理自w3school:http: www w3school com cn 1 获取当前日期:document write( Current time: +new Date());2 获取时间戳(毫秒):document write(new Date() getTime());3 设置年月日

本文代码整理自w3school:http://www.w3school.com.cn

1.获取当前日期:

document.write("Current time: "+new Date());
2.获取时间戳(毫秒):

document.write(new Date().getTime());
3.设置年月日(年为必选,月日为可选):

var d = new Date();
  d.setFullYear(2012,3,16)
  document.write(d);
  document.write("
"); document.write(d.getTime());
4.返回当前星期几:

document.write(new Date().getDay())


5.日期对象的比较:

function compareDate(){
    var oneday = new Date();
    oneday.setFullYear(2015,2,26);
    var currentday = new Date();

    if (oneday效果图:

\


示例代码:<喎"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;"> script 日期对象

(一)返回当前的日期

<script> document.write("Current time: "+new Date()); </script>

(二)返回时间戳(毫秒)

<script> document.write(new Date().getTime()); </script>

(三)设置年月日

<script> var d = new Date(); d.setFullYear(2012,3,16) document.write(d); document.write("
"); document.write(d.getTime()); </script>

(四)将当日的日期(根据 UTC)转换为字符串

<script> var d2 = new Date() document.write (d2.toUTCString()) </script>

(五)返回当前星期几

<script> var d3=new Date() var weekday=new Array(7) weekday[0]="星期日" weekday[1]="星期一" weekday[2]="星期二" weekday[3]="星期三" weekday[4]="星期四" weekday[5]="星期五" weekday[6]="星期六" document.write(weekday[d3.getDay()]) </script>

(六)显示一个时间钟表

<script> function startTime(){ var today = new Date(); var y = today.getFullYear(); var M = today.getMonth()+1; var day = today.getDay(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); m = checkTime(m); s = checkTime(s); var content = (y+"/"+M+"/"+day+" "+h+":"+m+":"+s).fontsize(6).fontcolor("red"); document.getElementById("time_text").innerHTML = content; setTimeout("startTime()",500); } function checkTime(i){ if(i<10){ return "0"+i; } return i; } </script>

(七)日期对象的比较

2015.3.26与当前时间的关系?

<script> function compareDate(){ var oneday = new Date(); oneday.setFullYear(2015,2,26); var currentday = new Date(); if (oneday





Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<