js時間操作

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

var myDate = new Date();    

myDate.getYear();       //擷取當前年份(2位)    

myDate.getFullYear();   //擷取完整的年份(4位,1970-????)  

myDate.getMonth();      //擷取當前月份(0-11,0代表1月)(要加1) 

myDate.getDate();       //擷取當前日(1-31)    

myDate.getDay();        // 擷取當前星期X(0-6,0代表星期天)    

myDate.getTime();       //擷取目前時間(從1970.1.1開始的毫秒數)    

myDate.getHours();      //擷取當前小時數(0-23)    

myDate.getMinutes();    // 擷取當前分鐘數(0-59)    

myDate.getSeconds();    //擷取當前秒數(0-59)    

myDate.getMilliseconds();   //擷取當前毫秒數(0-999)    

myDate.toLocaleDateString();    //擷取當前日期    

var mytime=myDate.toLocaleTimeString();    //擷取目前時間    

myDate.toLocaleString( );       //擷取日期與時間 

 

設定時間的方法:
setYear()       //改變年份
轉換時間的方法:

toGMTString() 把Date對象的日期(一個數值)轉變成一個GMT時間字串,返回類似下面的值:Weds,14 May 2007 10:02:02 GMT(精確的格式依賴於電腦上所啟動並執行作業系統而變) toLocaleString() 把Date對象的日期(一個數值)轉變成一個字串,使用所在電腦上配置使用的特定日期格式 UTC() 使用Date UTC(年、月、日、時、分、秒),以自從1997年1月1日00:00:00(其中時、分、秒是可選的)以來的毫秒數的形式返回日期

1、當前系統地區設定格式(toLocaleDateString和toLocaleTimeString)

例子:(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
結果: 2008年1月29日 16:13:11

2.一般字元串(toDateString和toTimeString)

例子: (new Date()).toDateString() + " " + (new Date()).toTimeString()
結果:Tue Jan 29 2008 16:13:11 UTC+0800

3.格林威治標準時間(toGMTString)

例子: (new Date()).toGMTString()
結果:Tue, 29 Jan 2008 08:13:11 UTC

4.全球標準時間(toUTCString)

例子: (new Date()).toUTCString()
結果:Tue, 29 Jan 2008 08:13:11 UTC

5.Date對象字串(toString)

例子: (new Date()).toString()
結果:Tue Jan 29 16:13:11 UTC+0800 2008

 

 

幾個需要注意的地方:

1、得到日期和年和設定日期和年時間,其中很怪的問題就是不能對月份進行設定(比較的怪):

<script type="text/javascript" language="javascript">//d = new Date();alert(d.toLocaleString());

d.setDate(14);

alert(d.toLocaleString());

d.setYear(2007);

alert(d.toLocaleString());

</script>

2、獲得年的時候最好用getFullYear()方法來做 3、由於針對月份,JS是從0開始的,因此需要對月份進
行操作時要加1 下面是幾個關於時間的經典而且經常會用到的例子,對大家會有協助的: 1、將2005-8
-5轉換成2005-08-05格式 

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

//

var strDate = ‘2005-8-5‘; 

 window.alert(strDate.replace(/\b(\w)\b/g, ‘0$1‘));  

</script>

2、得到間隔天數 <script type="text/javascript">//&amp;amp;lt;!--  alert("間隔天數為:"+(new Date(‘2005/8/15‘)-new Date(‘2003/9/18‘))/1000/60/60/24+"天")  //</script>

 

3、得到間隔時間 <script type="text/javascript">//var d1=new Date("2004/09/16 20:08:00");  var d2=new Date("2004/09/16 10:18:03");  var d3=d1-d2;  var h=Math.floor(d3/3600000);  var m=Math.floor((d3-h*3600000)/60000);  var s=(d3-h*3600000-m*60000)/1000;  alert("相差"+h+"小時"+m+"分"+s+"秒");  //</script>

4、得到今天的日期 <script type="text/javascript" language="javascript">//d = new Date();  alert(d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日");  //</script>

6、得到前N天或後 N天的日期 方法1: <script type="text/javascript">//function showdate(n)  {  var uom = new Date(new Date()-0+n*86400000);  uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();  return uom;  }  

window.alert(" 今天是:"+showdate(0));  window.alert("昨天是:"+showdate(-1));  window.alert("明天是:"+showdate(1));  window.alert("10天前是:"+showdate(-10));  window.alert("5天后是:"+showdate(5));  //</script>

方法 2: <script type="text/javascript">//function showdate(n)  {  var uom = new Date();  uom.setDate(uom.getDate()+n);  uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();  return uom;  }   

window.alert(" 今天是:"+showdate(0));  window.alert("昨天是:"+showdate(-1));  window.alert("明天是:"+showdate(1));  window.alert("10天前是:"+showdate(-10));  window.alert("5天后是:"+showdate(5));  //</script>

方法 3: <script type="text/javascript" language="Javascript">//Date.prototype.getDays=function(){  var _newDate=new Date();  _newDate.setMonth(_newDate.getMonth()+1);  _newDate.setDate(0);  $_days=_newDate.getDate();  delete _newDate;  return $_days;  }  function showdate(n)  {  var uom = new Date();  uom.setDate(uom.getDate()+n);  uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate()+"\n星期"+(‘天一二
三四五六 ‘.charAt(uom.getDay()))+"\n本月有"+ uom.getDays()+"天";  return uom;  }   window.alert("今天是:"+showdate(0));  window.alert("昨天是:"+showdate(-1));  window.alert("明天是:"+showdate(1));  window.alert("10天前是:"+showdate(-10));  window.alert("5天后是:"+showdate(5));  //</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.