最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)化為年月日時(shí)分秒'yyyy-MM-dd hh:mm:ss'的示例詳解

 更新時(shí)間:2024年02月04日 11:06:25   作者:唐屁屁兒  
這篇文章主要介紹了JS中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)化為年月日時(shí)分秒‘yyyy-MM-dd hh:mm:ss‘的相關(guān)知識(shí),通過示例代碼介紹了,Js各種時(shí)間轉(zhuǎn)換問題(YYYY-MM-DD 時(shí)間戳 中國標(biāo)準(zhǔn)時(shí)間),需要的朋友可以參考下

JS中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)化為年月日時(shí)分秒‘yyyy-MM-dd hh:mm:ss‘

新建一個(gè)formatDate.js文件,如下:

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}
export function formatDate(date, fmt) {
  if (!date) {
    return ''
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + ''
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      )
    }
  }
  return fmt
}
export default formatDate

如果是在vue項(xiàng)目中運(yùn)用,在main.js中全局引用

import formatDate from '@/utils/formatDate.js'
Vue.prototype.$formatDate = formatDate

再到頁面上直接調(diào)用$formatDate方法

// 獲取當(dāng)前時(shí)間年月日時(shí)分秒,即 2022-10-14 16:11:58
this.$formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
// 如果時(shí)分秒不需要精確到,即 2022-10-14 00:00:00
this.$formatDate(new Date(), 'yyyy-MM-dd 00:00:00')
// 意思就是說時(shí)分秒你需要什么時(shí)間就寫什么時(shí)間;需要是下午4點(diǎn)11分58秒,即 2022-10-14 16:11:58
this.$formatDate(new Date(), 'yyyy-MM-dd 16:11:58')

補(bǔ)充:

Js各種時(shí)間轉(zhuǎn)換問題(YYYY-MM-DD 時(shí)間戳 中國標(biāo)準(zhǔn)時(shí)間)

1. 類型總結(jié)

  • 指定格式 YYYY-MM-DD HH:MM:SS
  • 時(shí)間戳
  • 中國標(biāo)準(zhǔn)時(shí)間 Sat Jan 30 2022 08:26:26 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) new Date()獲得系統(tǒng)當(dāng)前時(shí)間就會(huì)是這種形式

2.類型之間的轉(zhuǎn)換

  • 時(shí)間戳轉(zhuǎn)換為 yyyy-mm-dd或yyyy-MM-dd HH-mm-ss
function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//時(shí)間戳為10位需*1000,時(shí)間戳為13位的話不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
        var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
        var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
        var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
        var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
    }

2.yyyy-mm-dd或yyyy-MM-dd HH-mm-ss 轉(zhuǎn)為時(shí)間戳

var stringTime = '2012-10-12 22:37:33';
//將獲取到的時(shí)間轉(zhuǎn)換成時(shí)間戳
var timestamp = Date.parse(new Date(stringTime));

3.中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)為 yyyy-mm-dd hh-mm-ss

 let y = date.getFullYear()
 let m = date.getMonth() + 1
 m = m < 10 ? ('0' + m) : m
 let d = date.getDate()
 d = d < 10 ? ('0' + d) : d
 let h =date.getHours()
 h = h < 10 ? ('0' + h) : h
 let M =date.getMinutes()
 M = M < 10 ? ('0' + M) : M
 let s =date.getSeconds()
 s = s < 10 ? ('0' + s) : s
 let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;

yyyy-mm-dd hh-mm-ss 轉(zhuǎn)為中國標(biāo)準(zhǔn)時(shí)間
1、new Date(“month dd,yyyy hh:mm:ss”);
2、new Date(“month dd,yyyy”);
3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:這種方式下,必須傳遞整型;
4、new Date(yyyy,mth,dd);
5、new Date(ms); 注意:ms:是需要?jiǎng)?chuàng)建的時(shí)間和 GMT時(shí)間1970年1月1日之間相差的毫秒數(shù);當(dāng)前時(shí)間與GMT1970.1.1之間的毫秒數(shù):var mills = new Date().getTime();

5.時(shí)間戳轉(zhuǎn)為中國標(biāo)準(zhǔn)時(shí)間

const time = 1531065600000;//時(shí)間戳(數(shù)字)
const youData = new Data(time);

6.中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)為時(shí)間戳

Date.parse(Time)

3. Date類型

創(chuàng)建日期對(duì)象 let now = new Date();

在不給Date構(gòu)造函數(shù)傳參數(shù)的情況下,創(chuàng)建的對(duì)象將保存當(dāng)前日期和時(shí)間。要基于其他日期和時(shí)間創(chuàng)建日期對(duì)象,需要傳入毫秒表示。

方法:Date.parse() && Date.UTC() && Date.now() && Date.toLocaleString() && Date.toString()

Date.parse()
支持的參數(shù)類型:
1) 月/日/年 eg:’1/18/2023‘

2) 月名 日,年 eg: ‘May 23, 2019’

3) 周幾 月名 日 年 時(shí):分:秒 時(shí)區(qū) eg:’Wed Jan 18 2023 16:21:53 GMT+0800‘

4) YYYY-MM-DDTHH:mm:ss.sssZ eg: 2019-05-23T00:00:00

如果傳入的參數(shù)不表示日期,則返回NaN

用法

Date.UTC()
2000年1月1日零點(diǎn)

2005年5月5日下午5點(diǎn)55分55秒(注意月份是0為起點(diǎn)的)

Date.now() 當(dāng)前時(shí)間

Date.toLocaleString() && Date.toString()

4. 日期格式化

toDateString()

toTimeString()

toLocaleDateString()

toLocaleTimeString()

toUTCString()

5. 如何判斷是否為當(dāng)天時(shí)間

if (new Date(str).toDateString() === new Date().toDateString()) {
    //今天
    console.log("當(dāng)天");
} else if (new Date(str) < new Date()){
    //之前
    console.log(new Date(str).toISOString().slice(0,10));
}

到此這篇關(guān)于JS中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)化為年月日時(shí)分秒‘yyyy-MM-dd hh:mm:ss‘的文章就介紹到這了,更多相關(guān)js中國標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)換年月日時(shí)分秒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

铜鼓县| 桑植县| 民乐县| 固镇县| 凤台县| 高平市| 延津县| 观塘区| 保靖县| 平湖市| 安陆市| 吉林市| 商河县| 襄樊市| 纳雍县| 子洲县| 托克托县| 漾濞| 黄浦区| 调兵山市| 思南县| 岱山县| 南投市| 罗山县| 绍兴县| 雷山县| 娄底市| 沭阳县| 乌恰县| 从化市| 岳池县| 广南县| 北川| 喀什市| 西乡县| 获嘉县| 桂阳县| 三亚市| 华安县| 谷城县| 清水河县|