JS how to get system time month Day

Source: Internet
Author: User

JavaScript comes with an object (constructor), Date (). Here's the code:

回答一:

varnow = newDate(); 

var nowTime = now.toLocaleString();  var date = nowTime.substring(0,10); //截取日期  var time = nowTime.substring(10,20);  //截取时间  var week = now.getDay();  //星期  var hour = now.getHours();  //小时 Answer two:

vardate = newDate();

varyear = date.getFullYear();

var month = date.getMonth()+1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); alert(year+ ‘年‘ +month+ ‘月‘ +day+ ‘日 ‘ +hour ‘:‘ +minute+ ‘:‘ +second)Answer three: function showTime() {     var  now =  new  Date();     var  nowTime = now.toLocaleString();     var  date = nowTime.substring(0,10); //截取日期     var  time = nowTime.substring(10,20);  //截取时间     var  week = now.getDay();  //星期     var  hour = now.getHours();  //小时     //判断星期几     var weeks = [ "日" , "一" , "二" , "三" , "四" , "五" , "六" ];     var getWeek =  "星期" + weeks[week];     var sc;     //判断是AM or PM     if (hour >= 0 && hour < 5){      sc =  ‘凌晨‘       else if (hour > 5 && hour <= 7){      sc =  ‘早上‘       else if (hour > 7 && hour <= 11){      sc =  ‘上午‘       else if (hour > 11 && hour <= 13){      sc =  ‘中午‘       else if (hour> 13 && hour <= 18){      sc =  ‘下午‘       else if (hour > 18 && hour <= 23){      sc =  ‘晚上‘       document.getElementById( ‘time‘ ).innerHTML = "当前时间:" + date+ " " + getWeek + " " + "   " +sc+ "  " +time;    setTimeout( ‘showTime()‘ ,1000);  </script>  调用方法:  <body onload= "showTime()" <div id= "time" ></div>  </body> 

JS how to get system time month Day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.