国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

JS實(shí)現(xiàn)獲取毫秒值及轉(zhuǎn)換成年月日時(shí)分秒的方法

2019-11-19 13:15:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了JS實(shí)現(xiàn)獲取毫秒值及轉(zhuǎn)換成年月日時(shí)分秒的方法。分享給大家供大家參考,具體如下:

時(shí)間日期對(duì)象

創(chuàng)建方式一

var date=new Date();

創(chuàng)建方式二 兼容性最強(qiáng) 推薦使用

var date1=new Date("2017/08-26 15:15:15");

創(chuàng)建方式三

var date2=new Date(2017,9,18,23,15,23);

返回結(jié)果是從1970/01/01到現(xiàn)在的毫秒值

var date = Date.now();var date = +new Date();var date = new Date().getTime();var date = new Date().valueOf();

把日期解析成毫秒值

var date=Date.parse("2017-05-24 18:23:14");

傳入毫秒值 獲取該毫秒值對(duì)應(yīng)的時(shí)間日期

var date =new Date(Date.now());

getDate() 獲取日 1-31
getDay () 獲取星期 0-6(0代表周日)
getMonth () 獲取月 0-11(1月從0開始)
getFullYear () 獲取完整年份(瀏覽器都支持)
getHours () 獲取小時(shí) 0-23
getMinutes () 獲取分鐘 0-59
getSeconds () 獲取秒 0-59
getMilliseconds () 獲取毫秒 (1s = 1000ms)
getTime () 返回累計(jì)毫秒數(shù)(從1970/1/1午夜)

自己簡(jiǎn)單封裝的輸入毫秒值,根據(jù)不同的參數(shù)獲取年月日時(shí)分秒

獲取你想要的時(shí)間格式 參數(shù)(毫秒值,int) int取值為:0:年1:月2:日3:時(shí)4:分5:秒
如:

console.log(getTime(1523604904000,1));function getTime(second,getDateType) {  var date =new Date(second);  if(getDateType==0){    return date.getFullYear();  }else if(getDateType==1){    if((date.getMonth()+1)<=9){      return "0"+(date.getMonth()+1);    }else {      return date.getMonth()+1;    }  }else if(getDateType==2){    if(date.getDate()<=9){      return "0"+date.getDate();    }else {      return date.getDate();    }  }else if(getDateType==3){    if(date.getHours()<=9){      return "0"+date.getHours();    }else {      return date.getHours();    }  }else if(getDateType==4){    if(date.getMinutes()<=9){      return "0"+date.getMinutes();    }else {      return date.getMinutes();    }  }else if(getDateType==5){    return date.getSeconds ();  }else {    alert("輸入時(shí)間格式有誤!");    return;  }}

這里使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.VeVB.COm/code/HtmlJsRun,測(cè)試結(jié)果如下:

PS:這里再為大家推薦幾款比較實(shí)用的天數(shù)計(jì)算在線工具供大家使用:

在線日期/天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi

在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/datecalc

在線日期天數(shù)差計(jì)算器:
http://tools.VeVB.COm/jisuanqi/onlinedatejsq

在線天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/datejsq

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript時(shí)間與日期操作技巧總結(jié)》、《JavaScript+HTML5特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无极县| 乌鲁木齐市| 德令哈市| 新疆| 和平区| 民勤县| 凤阳县| 巴楚县| 沁源县| 玉林市| 青海省| 将乐县| 扶沟县| 扬中市| 和龙市| 漳州市| 瓮安县| 祁东县| 乌审旗| 湟中县| 宾阳县| 公主岭市| 孟村| 昭通市| 赣榆县| 仁布县| 开封县| 聂拉木县| 乐东| 嘉荫县| 濮阳县| 武胜县| 宁河县| 安泽县| 呼和浩特市| 揭东县| 鸡东县| 潮州市| 加查县| 泰安市| 尉犁县|