欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS?Date時間格式化的方法

 更新時間:2024年01月20日 10:50:34   作者:請叫我彥祖  
這篇文章主要介紹了JS?Date時間格式化的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

JS Date時間格式化

1.1日期的格式化

1.1.1方法一(格式,日期))

// 入參 fmt-格式 date-日期
function dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 時
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以繼續(xù)添加,必須轉化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}

舉個例子:

let date = new Date()
dateFormat("YYYY-mm-dd HH:MM:SS", date)
console.log(dateFormat("YYYY-mm-dd HH:MM", date));
輸出: 2019-06-06 19:45:35

1.1.2方法二(當前日期)

// 入參 fmt-格式
Date.prototype.format = function(fmt){
  var o = {
    "M+" : this.getMonth()+1,                 //月份
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小時
    "m+" : this.getMinutes(),                 //分
    "s+" : this.getSeconds(),                 //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S"  : this.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt)){
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  }
  for(var k in o){
    if(new RegExp("("+ k +")").test(fmt)){
      fmt = fmt.replace(
        RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
    }       
  }
  return fmt;
}

舉個例子:

var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//2021-06-03 19:17:35
console.log(nowStr);
//2021年06月03日
console.log(new Date().format("yyyy年MM月dd日")); 
var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); 
//2021-06-03 19:17:35
console.log(nowStr);//
//2021年06月03日19小時17分35秒
console.log(new Date().format("yyyy年MM月dd日hh小時mm分ss秒"));
//輸出:
2021-06-03 19:17:35
2021年06月03日
2021-06-03 19:17:35
2021年06月03日19小時17分35秒

推薦時間插件庫:moment.js 

Js Date日期格式和字符串的相互轉化

// 日期格式化
	 Date.prototype.format = function(fmt) { 
		var o = { 
			"M+" : this.getMonth()+1,                 //月份 
			"d+" : this.getDate(),                    //日 
			"h+" : this.getHours(),                   //小時 
			"m+" : this.getMinutes(),                 //分 
			"s+" : this.getSeconds(),                 //秒 
			"q+" : Math.floor((this.getMonth()+3)/3), //季度 
			"S"  : this.getMilliseconds()             //毫秒 
		}; 
		if(/(y+)/.test(fmt)) {
				fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
		}
		for(var k in o) {
			if(new RegExp("("+ k +")").test(fmt)){
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
			}
		}
		return fmt; 
	}
let time = new Date().format("yyyy-MM-dd hh:mm:ss");

到此這篇關于JS Date時間格式化的文章就介紹到這了,更多相關JS Date時間格式化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論