JS格式化时间的⼏种⽅法总结⽅法⼀
⽅法
//格式化时间
function format(dat){
//获取年⽉⽇,时间
var year = FullYear();
var mon = (Month()+1) < 10 ? "0"+(Month()+1) : Month()+1;
var data = Date()  < 10 ? "0"+(Date()) : Date();
var hour = Hours()  < 10 ? "0"+(Hours()) : Hours();
var min =  Minutes()  < 10 ? "0"+(Minutes()) : Minutes();
var seon = Seconds() < 10 ? "0"+(Seconds()) : Seconds();
var newDate = year +"-"+ mon +"-"+ data +" "+ hour +":"+ min +":"+ seon;
return newDate;
}
调⽤
//获取时间
var dat = new Date();
//格式化时间
var newDate = format(dat);
⽅法⼆
/**
* 格式化⽇期
* @param {string | number | Date} value 指定⽇期
* @param {string} format 格式化的规则
* @example
* ```js
* formatDate();
* formatDate(1603264465956);
* formatDate(1603264465956, "h:m:s");
* formatDate(1603264465956, "Y年M⽉D⽇");
* ```
*/
function formatDate(value = w(), format = "Y-M-D h:m:s") {
const formatNumber = n => `0${n}`.slice(-2);
const date = new Date(value);
const formatList = ["Y", "M", "D", "h", "m", "s"];
const resultList = [];
resultList.FullYear().toString());
resultList.push(Month() + 1));
resultList.push(Date()));
resultList.push(Hours()));
resultList.push(Minutes()));
resultList.push(Seconds()));
for (let i = 0; i < resultList.length; i++) {
format = place(formatList[i], resultList[i]);
}
return format;
}
⽅法三
获得当前时间,并格式化
function getNowFormatDate() {
var date = new Date();
var year = FullYear();
var month = Month() + 1;
var d = Date();
var hour = Hours();
var minute = Minutes();
var second = Seconds();
if(month<10){
month = "0" + month;
}
if(d<10){
d = "0" + d;
}
if(hour<10){
hour = "0" + hour;
}
if(minute<10){
minute = "0" + hour;
}
if(second<10){
second = "0" + second;
}
return year + "-" + month + "-" + d + " " +hour + ":" + minute + ":" + second;
}
注意这⾥的⽉要加1。
⽅法四
⽅法
// 对Date的扩展,将 Date 转化为指定格式的String
// ⽉(M)、⽇(d)、⼩时(h)、分(m)、秒(s)、季度(q) 可以⽤ 1-2 个占位符,
// 年(y)可以⽤ 1-4 个占位符,毫秒(S)只能⽤ 1 个占位符(是 1-3 位的数字)
// 例⼦:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": Month() + 1, //⽉份
"d+": Date(), //⽇
"h+": Hours(), //⼩时
"m+": Minutes(), //分
"s+": Seconds(), //秒
"q+": Math.floor((Month() + 3) / 3), //季度
"S": Milliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = place(RegExp.$1, (FullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = place(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));    return fmt;
}
调⽤
formatnumber数字格式$(document).ready(function(){
//给时间控件加上样式
$("#tb").find("input[name='cruMon_begin']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});});
$("#tb").find("input[name='cruMon_end']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});});
$("#tb").find("input[name='cruMon_begin']").attr("value",new Date().Format("yyyy-MM"));
$("#tb").find("input[name='cruMon_end']").attr("value",new Date().Format("yyyy-MM"));
});
⽅法五
Date.prototype.format = function(format) {
var o = {
"M+": Month() + 1, //month
"d+": Date(), //day
"h+": Hours(), //hour
"m+": Minutes(), //minute
"s+": Seconds(), //second
"q+": Math.floor((Month() + 3) / 3), //quarter
"S": Milliseconds() //millisecond
}
if (/(y+)/.test(format)) format = place(RegExp.$1,
(FullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(format))
format = place(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
return format;
}
说明:获取到时间var time = new Date().format("yyyy-MM-dd hh:mm:ss");
到此这篇关于JS格式化时间的⽂章就介绍到这了。希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。