js寫一個簡單的倒計時
阿新 • • 發佈:2018-12-22
一. 首先,我們要了解一下js對時間的基礎處理函式
函式名 功能
getYear() 獲取四位數的年份
getMonth() 獲取2位數的月數, 這個是從 0 開始的 , 注意 不是從1 開始的!!!
getDate() 獲取2位數的日 數, 也是從1 開始的
getDay() 獲取表示 星期的數字, 注意星期天返回的是0, …
getHours() 獲取小時數…
getMinites() …
getSeconds() …
**getTime() 獲取dateobject 到 1970年 1月1日 0點0時0分 的毫秒數
** valueOf() 反過來,獲取日期物件的毫秒數
** Date.parse() 反過來, 獲取…毫秒數
轉換成字串的函式 …
toString() 全部轉變成 字串…
toDateString() 只轉變日期字串
toTimeString() 只轉變時間字串
二.程式碼效果以及原始碼
<template> <div class="date"> <div>距離{{hours}}點秒殺場,還有:</div> <div>{{Minutes}}:</div> <div>{{Seconds}}</div> </div> </template> <script> export default { data(){ return{ hours:'', Minutes:'', Seconds:'', } }, methods:{ CallBack(){ let nowDate= new Date; this.date=nowDate; this.Minutes=(60-nowDate.getMinutes()-1).toString(); this.Seconds=(60-nowDate.getSeconds()).toString(); } }, created(){ let nowDate= new Date; this.hours=nowDate.getHours()+1 }, mounted(){ let timer= setInterval(this.CallBack,1000) } } </script> <style lang="less"> .date{ display: flex } </style>