javascript收藏之日期

來源:互聯網
上載者:User

 
    <script type="text/javascript">
    //getTime()指1970.1.1到現在過了多少秒
        var n = new Date();
        var t = n.getFullYear();
        //document.write('my time:'+n.getFullYear()+' '+n.getMonth()+' '+n.getDate()+' '+n.getDay()+' 'n.getHours()+' '+n.getMinutes()+' '+n.getSeconds());
        document.write('<br>');
        document.write(t);
        document.write('<br>');    
        
        function printTime(secs){
            var sep = ':';
               var hours,minutes,seconds,time;
               var now = new Date();
               hours = now.getHours();
               if(hours<12){
                   meridiem = 'am';
               }else{
                   meridiem = 'pm';
               }
               hours = hours%12;
               if(hours==0){
                   hours=12;
               }
                time = hours;
                minutes = now.getMinutes();
                if(minutes<10){//改成10就不可以了!!!!!!!!!!!!!!!!!!!!!!!!
                    minutes = '0' + minutes;
                }
                time +=sep+minutes;
                if(secs){
                    seconds = now.getSeconds();
                    if(seconds<10){
                        seconds = '0'+seconds;
                    }
                    time +=sep+seconds;
                }
               return time+' '+meridiem;
       
           }
       //new Date(),'%N/%D/%Y'
       function dateString(date,string){
               var year = date.getFullYear();
               var month = date.getMonth();
               var realMonth = month+1;
               var fillMonth = realMonth;
               if(realMonth<10){
                   fillMonth = '0'+fillMonth;
               }
               var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
               var monthName = months[parseInt(month,10)-1];
               
               var day = date.getDate();
               var fillDate = day;
               if(day<10){
                   fillDate = '0'+day;
               }
               var weekday = date.getDay();
               var weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
               var dayName = weekdays[weekday];
               
               string = string.replace(/%Y/g,year);
               string = string.replace(/%y/g,year.toString().slice(-2));//年份後兩位
               string = string.replace(/%M/g,monthName);
               string = string.replace(/%m/g,monthName.slice(0,3));
               string = string.replace(/%N/g,fillMonth);
               string = string.replace(/%n/g,realMonth);
               string = string.replace(/%W/g,dayName);
               string = string.replace(/%m/g,dayName.slice(0,3));
               string = string.replace(/%D/g,fillDate);
               string = string.replace(/%d/g,day);
               
               return string;
       }
       
       
        document.write('<br>');    
           document.write(printTime(true));
           document.write('<br>');
           document.write(dateString(new Date(),'Today is %W, %M %d, %Y'));
    </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.