날짜 문자열로 변환

문자열로 날짜

<p의 ID = "데모"> </ p>

<스크립트 유형 = "텍스트 / 자바 스크립트">

        VAR 상자 = 새로운 날짜 ();

                   document.write를 (box.toUTCString ());

         경고 (box.toString ());

 

          VAR myVar에 setInterval을 = (MyTimer로 1000);

 

          기능을 MyTimer () {

                 VAR의 새로운 D = 일 ();

                 document.getElementById를 ( "데모") innerHTML을 = d.toLocaleTimeString ().;

          }

 

문자열로 날짜 :

          toString ()이 문자열로 Date 객체로 변환합니다. 기본

          문자열로 날짜 오브젝트 toTimeString () 시간 부분

          문자열로 객체의 일 부분 () 날짜 toDateString ()과

          toUTCString ()는 월드 타임에 따르면, Date 객체는 문자열로 변환

          toLocaleString ()는 현지 시각 포맷에 따라 문자열로 날짜 오브젝트 변환 로컬

          toLocaleTimeString () 로컬 시간 형식 문자열로 Date 객체의 시간 부분에있어서.

          toLocaleDateString () 로컬 시간 형식 문자열에 날짜 오브젝트 날짜 부분에있어서.

************************************************** *********************************

날짜 날짜 형식 공통 출력

<스타일>

       #내 시간 {

              배경 : # 000;

              색상 : #fff;

              텍스트 정렬 : 센터;

}

</ 스타일>

<body>

     <H1 ID = "MYTIME"> 시간 표시 </ H1> 

     <script>

              // 경고 (습관);

              하여 setInterval (함수 () {//하여 setInterval () 메소드는 밀리 초 (소정 간격으로 호출 될 수있다) 계산식 또는 함수

                      VAR 습관 = 새로운 날짜 ();

                      연도 = habit.getFullYear ();

                      개월 habit.getMonth = () + 1;

                      월 = checkTime (월);

                      일 habit.getDate = ();

                      week = habit.getDay();

                      switch(week) {

                             case 1:

                                    week = "星期一";

                                    break;

                             case 2:

                                    week = "星期二";

                                    break;

                                      case 3:

                                      week = "星期三";

                                      break;

                               case 4:

                                      week = "星期四";

                                      break;

                               case 5:

                                      week = "星期五";

                                      break;

                               case 6:

                                      week = "星期六";

                                      break;

                               default:

                                      week = "星期日";

                                      break;

}

hour = habit.getHours(),

minute = habit.getMinutes();

minute = checkTime(minete);

second = habit.getSeconds();

second = checkTime(second);

time = (year + '-' + month + '-' + day + ' ' + week + ' ' + hour + ':' + minute + ':' + second);

var obj = document.getElementById('mytime');

obj.innerHTML = time;

//document.title = time;

},1000)

 

function checkTime(i){ //封装一个数字在小于10时在前面加零

       if(i<10){

          i = "0" + i

       }//在数字<10之前加零

       return i;

}

     </script>

</body>

*********************************************************************************

Date获得和设置

<script type = "text/javascript">

        var box = new Date(2019,7,19,13,19,45,30);

                         alert(box);               //Mon Aug 19 2019 13:19:45   GMT+0800(中国标准时间)

                         alert('toString:' + box.toLocaleString());   //toLocaleString:2019/8/19下午1:19:45

                         alert('valueOf:' + box.valueOf());   //valueof:1566191985030

        var box = new Date();

        document.write(box);

        //box.setMonth(11);

  

        //alert(box.getMonth()+1);

 

        //toLocaleTimeString()           根据本地时间输出本地时间   下午1:19:45

        //box.toLocaleDateString()        根据本地年月日输出本地年月日  结果:2019/8/19

</script>

***********************************************************************************

JS里面获取和设置日期的方法:

          Date   对象用于处理日期和时间 返回当前的日期和时间。

             Date   对象会自动把当前日期和时间保存为其初始值。

             获取毫秒数:

                 1、Date.parse()是Date对象的静态方法  获得的是1970年1月1日零时到现在的毫秒数

                      2、alert('valueOf:' + box.valueOf());      返回Date对象的原始值以毫秒数

                      3、Date。UTC()     根据世界时返回1970年1月1日到指定日期的毫秒数。

                      4、getTime()   返回1970年1月1日至今的毫秒数。

                      5、Number(new Date("2019-04-15"));   日期转换为数字也是1970年1月1日至今的毫秒数。

             获取时间:

                 1、var box = new Date(1566191985030);

                      2、var box = new Date('2019/8/19');

                      3、var box = new Date(2019,7,19,13,19,45,30);

              获取方法用于获取日期的某个部分  下面是最常用的方法:

                 getDate()                以数值返回天(1-31)

                      getDay()                 以数值获取周名(0-6)   //注意0星期日

                      getFullYear()           获取四位的年(yyyy)

                      getHours()              获取小时(0-23)

                      getMilliseconds()    获取毫秒(0-999)

                      getMinutes()           获取分(0-59)

                      getMonth()+1         获取月(0-11)   要加1才是最终的月份数

                      getSeconds()           获取秒(0-59)

                      getTime()                 获取时间(从1970年1月1日至今)

 

                      getUTCDate()           等于getDate(),但返回UTC 日期

                      getUTCDay()            等于getDay(),但返回UTC 日

                      getUTCFullYear()      等于getFullYear(),但返回UTC年

                      getUTCHours()         等于getHours(),但返回UTC 小时

                      getUTCMilliseconds()        等于getMilliseconds(),但返回UTC毫秒

                      getUTCMInutes()      等于getMInutes(),但返回UTC分

                      getUTCMonth()        等于getMonth(),但返回UTC月

                      getUTCSeconds()     等于getSeconds(),但返回UTC秒

**********************************************************************************

getHours()和getUTCHours()相差8小时         东八区由八个小时的时差。

 

getTimezoneOffset()   返回本地时间与格林威治标准时间(GMT)   的分钟差。

**********************************************************************************

设置方法用于设置日期的某个部分 下面是最常用的方法:

              var box = new Date();

                  box.setMonth(11);

                  alert(box);

                  

                  setDate()                                  设置Date 对象中月的某一天(1~31)。

                  setMonth()                               设置Date 对象中月份(0~11).

                  setFullYear()                             设置Date 对象中的年份(四位数字)

                  setHours()                                设置Date 对象中的小时(0~23)。

                  setMinutes()                             设置Date 对象中的分钟(0~59)。

                  setSeconds()                            设置Date 对象中的秒钟(0~59)。

                  setMilliseconds()                      设置Date 对象中的毫秒(0~999)。

                  setTime()                                  以毫秒设置Date 对象

 

                  setUTCDate()                 根据世界时设置Date 对象中月份的一天(1~31)

                  setUTCMonth()              根据世界时设置Date 对象中的月份(0~11)

                  setUTCFullYear()            根据世界时设置Date 对象中的年份(四位数字)

                  setUTCHours()               根据世界时设置Date 对象中的小时(0~23)

                  setUTCMinutes()            根据世界时设置Date 对象中的分钟(0~59)

                  setUTCSeconds()           根据世界时设置Date 对象中的秒钟(0~59)

                  setUTCMilliseconds()     根据世界时设置Date 对象中的毫秒(0~999)

                  var today,someday,text;

                        today = new Date();

                        someday = new Date();

                        someday.setFullTear(2049,0,16);

              

                        if (someday > today) {

                            text = "今天在2049年1月16日之前";

                        } else {

                           text = "今天在2049年1月16日之后";                           

                        }

                        alert(text);

추천

출처www.cnblogs.com/wzy123/p/11390704.html