一個簡單的 js 時間對象創建

JS中獲取時間很常見,湊湊熱鬧,也獲取一個時間對象試試

首先,先了解js的獲取時間函數如下:

var?myDate = new Date();? ? ? ? ? //創建一個時間對象

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

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

myDate.getMonth();? ? ? ? ? ? ? ? ? ?//?獲取當前月份(0--11,? ?0 代表1月)

myDate.getDate();? ? ? ? ? ? ? ? ? ? ? //?獲取當前是哪一日 (1----31)

myDate.getDay();? ? ? ? ? ? ? ? ? ? ? ?//?獲取當前是哪一天,即星期幾(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();? ? ?//?獲取當前時間的日期

myDate.toLocaleTimeString();? ? ?//?獲取當前時間

myDate.toLocaleString();? ? ? ? ? ? ?//獲取當前日期與時間

?

創建一個時間如下:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="UTF-8">
 5   <title>Document</title>
 6   <style>
 7     .box{
 8       margin:200px auto;
 9       width:400px;
10     }
11     span,code{
12        display: inline-block;
13        font-size: 75px;
14     }
15   </style>
16   <script>
17       window.onload = function(){
18 
19         function OTime(){
20           this.aspan = document.querySelectorAll('.box span');
21         };
22         OTime.prototype.todouble = function( n ){
23           return n>10 ? '' + n : '0'+ n ;
24         };
25         OTime.prototype.getcurtime = function(){
26           var odate = new Date();
27           var OHour = odate.getHours();
28           var OMin = odate.getMinutes();
29           var OSec = odate.getSeconds();
30           return this.todouble( OHour ) + this.todouble( OMin ) + this.todouble( OSec ) ;
31         };
32         OTime.prototype.setcurTime = function(){
33           var str = this.getcurtime();
34           for(var i=0;i<this.aspan.length;i++){
35             this.aspan[i].innerHTML = str.charAt( i );
36           };
37         };
38         OTime.prototype.showtime = function(){
39           this.setcurTime();
40           var that = this ;
41           setInterval(function(){
42             that.setcurTime();
43           },1000);
44         };
45         var curtime = new OTime();
46         curtime.showtime();
47 
48       };
49   </script>
50 </head>
51 <body>
52   <div class="box">
53       <span>2</span>
54       <span>2</span>
55       <code>:</code>
56       <span>1</span>
57       <span>1</span>
58       <code>:</code>
59       <span>3</span>
60       <span>3</span>
61   </div>
62 </body>
63 </html>

?

運行結果:

?

轉載于:https://www.cnblogs.com/huanying2015/p/8018407.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390569.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390569.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390569.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

裁判打分_內在的裁判偏見

裁判打分News flash: being an umpire is hard. Their job is to judge whether a ball that’s capable of moving upwards of 100 MPH or breaking 25 inches crossed through an imaginary zone before being caught. I don’t think many would argue that they have it ea…

數據庫sql課程設計_SQL和數據庫-初學者完整課程

數據庫sql課程設計In this course, Mike Dane will teach you database management basics and SQL.在本課程中&#xff0c;Mike Dane將教您數據庫管理基礎知識和SQL。 The course starts off with Mike helping you install MySQL on Windows or Mac. Then he explores topic…

LCP 07. 傳遞信息

小朋友 A 在和 ta 的小伙伴們玩傳信息游戲&#xff0c;游戲規則如下&#xff1a; 有 n 名玩家&#xff0c;所有玩家編號分別為 0 &#xff5e; n-1&#xff0c;其中小朋友 A 的編號為 0 每個玩家都有固定的若干個可傳信息的其他玩家&#xff08;也可能沒有&#xff09;。傳信息…

微信公眾號自動回復加超鏈接最新可用實現方案

你在管理微信號時是否會有自動回復或者在關鍵字觸發自動回復加一個超鏈接的需求呢&#xff1f;例如下圖像王者榮耀這樣&#xff1a; 很多有開發經驗的朋友都知道微信管理平臺會類似富文本編輯器&#xff0c;第一想到的解決方案會是在編輯框中加<a href網址 >顯示文字<…

devops開發模式流程圖_2020 Web開發人員路線圖–成為前端,后端或DevOps開發人員的視覺指南

devops開發模式流程圖There are many ways you can go about picking up the skills you need to become a developer.您可以采用多種方法掌握成為開發人員所需的技能。 There are linear curriculums that teach you a bit of everything - like freeCodeCamps full stack de…

從Jupyter Notebook切換到腳本的5個理由

意見 (Opinion) 動機 (Motivation) Like most people, the first tool I used when started learning data science is Jupyter Notebook. Most of the online data science courses use Jupyter Notebook as a medium to teach. This makes sense because it is easier for be…

leetcode 1833. 雪糕的最大數量

夏日炎炎&#xff0c;小男孩 Tony 想買一些雪糕消消暑。 商店中新到 n 支雪糕&#xff0c;用長度為 n 的數組 costs 表示雪糕的定價&#xff0c;其中 costs[i] 表示第 i 支雪糕的現金價格。Tony 一共有 coins 現金可以用于消費&#xff0c;他想要買盡可能多的雪糕。 給你價格…

MVC架構 -- 初學試水選課管理系統

項目文件網站地址&#xff1a;http://www.gegecool.cn:90/ 第一次對MVC 進行轉載于:https://www.cnblogs.com/wtusoso/p/8032120.html

rest api 示例2_REST API教程– REST Client,REST Service和API調用通過代碼示例進行了解釋

rest api 示例2Ever wondered how login/signup on a website works on the back-end? Or how when you search for "cute kitties" on YouTube, you get a bunch of results and are able to stream off of a remote machine?有沒有想過網站上的登錄/注冊在后端如…

win10子系統linux編譯ffmpeg

android-ndk-r14b(linux版) ffmpeg-4.0 開啟win10子系統&#xff08;控制面板-》程序和功能-》啟用或關閉Windows功能 然后在 適用與 Linux 的 Windows 子系統前面打勾&#xff09; 然后點擊確定&#xff0c;等待安裝&#xff0c;電腦會重啟 然后在win10應用商店 搜索ubuntu安裝…

ip登錄打印機怎么打印_不要打印,登錄。

ip登錄打印機怎么打印Often on Python, especially as a beginner, you might print( ) a variable in order to see what is happening in your program. It is possible if you rely on too many print statements throughout your program you will face the nightmare of h…

leetcode 451. 根據字符出現頻率排序

給定一個字符串&#xff0c;請將字符串里的字符按照出現的頻率降序排列。 示例 1:輸入: "tree"輸出: "eert"解釋: e出現兩次&#xff0c;r和t都只出現一次。 因此e必須出現在r和t之前。此外&#xff0c;"eetr"也是一個有效的答案。 示例 2:輸入…

Spring-Security 自定義Filter完成驗證碼校驗

Spring-Security的功能主要是由一堆Filter構成過濾器鏈來實現&#xff0c;每個Filter都會完成自己的一部分工作。我今天要做的是對UsernamePasswordAuthenticationFilter進行擴展&#xff0c;新增一個Filter&#xff0c;完成對登錄頁面的校驗碼的驗證。下面先給一張過濾器的說明…

如何使用Ionic和Firebase在短短三天內創建冠狀病毒跟蹤器應用程序

I am really fond of Hybrid App technologies – they help us achieve so much in a single codebase. Using the Ionic Framework, I developed a cross-platform mobile solution for tracking Coronavirus cases in just 3 days. 我真的很喜歡Hybrid App技術-它們可以幫助…

二、Java面向對象(7)_封裝思想——this關鍵字

2018-04-30 this關鍵字 什么是this: 表示當前對象本身&#xff0c;或當前類的一個實例&#xff0c;通過 this 可以調用本對象的所有方法和屬性。 this主要存在于兩個地方&#xff1a; 1&#xff09;構造函數&#xff1a;此時this表示調用當前創建的對象 2&#xff09;成員方法中…

機器學習模型 非線性模型_調試機器學習模型的終極指南

機器學習模型 非線性模型You’ve divided your data into a training, development and test set, with the correct percentage of samples in each block, and you’ve also made sure that all of these blocks (specially development and test set) come from the same di…

leetcode 645. 錯誤的集合

集合 s 包含從 1 到 n 的整數。不幸的是&#xff0c;因為數據錯誤&#xff0c;導致集合里面某一個數字復制了成了集合里面的另外一個數字的值&#xff0c;導致集合 丟失了一個數字 并且 有一個數字重復 。 給定一個數組 nums 代表了集合 S 發生錯誤后的結果。 請你找出重復出…

Linux環境變量總結

現在每天測試到時候會與Linux打交道&#xff0c;自然也會用到環境變量了。看了網上幾篇文章&#xff0c;結合自己到實踐和看法&#xff0c;總結以下Linux的環境變量吧。一、什么是環境變量&#xff1f;環境變量相當于給系統或用戶應用程序設置的一些參數, 具體起什么作用這當然…

目錄指南中的Python列表文件-listdir VS system(“ ls”)通過示例進行解釋

&#x1f539;歡迎 (&#x1f539; Welcome) If you want to learn how these functions work behind the scenes and how you can use their full power, then this article is for you.如果您想了解這些功能在后臺如何工作以及如何充分利用它們的功能&#xff0c;那么本文適合…

Java多線程并發學習-進階大綱

1、synchronized 的實現原理以及鎖優化&#xff1f;2、volatile 的實現原理&#xff1f;3、Java 的信號燈&#xff1f;4、synchronized 在靜態方法和普通方法的區別&#xff1f;5、怎么實現所有線程在等待某個事件的發生才會去執行&#xff1f;6、CAS&#xff1f;CAS 有什么缺陷…