版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tel13259437538/article/details/80659104
Write By Monkeyfly
以下内容均为原创,如需转载请注明出处。
前提
- 大家平时可能会有各种各样的时间格式的要求,在此,我将项目里学到的关于时间格式化的方法分享出来,供大家参考学习。
- 这会为大家日后的开发提供很大的便利。在需要的时候,直接传参调用该方法即可。
- 该方法罗列出了我们平时常用到的一些时间格式。
- 只要为该方法传递不同的格式参数,就会输出不同的时间格式。
方法
具体代码如下所示:
/**
* 获取时间 格式为:format
* format 参数为时间格式
* 注:若方法返回的值是一个两位的数字。但是,返回值不总是两位的,如果该值小于 10,则仅返回一位数字。
*/
function getLocaleDateTime(format){
var date = new Date();
var getyear = date.getFullYear();//返回一个表示年份的 4 位数字。
var getmonth = date.getMonth()+1;//返回表示月份的数字,返回值是 0(一月) 到 11(十二月) 之间的一个整数。
var getday = date.getDate();//返回月份的某一天,返回值是 1 ~ 31 之间的一个整数。
var gethour = date.getHours();//返回值是 0 (午夜) 到 23 (晚上 11 点)之间的一个整数。
var getminute = date.getMinutes();//返回值是 0 ~ 59 之间的一个整数。
var getsecond = date.getSeconds();//返回值是 0 ~ 59 之间的一个整数。
//不过返回值不总是两位的,如果返回的值小于 10,则仅返回一位数字。故进行加“0”操作。
var getMonth = changeTime(getmonth);
var getDay = changeTime(getday);
var getMinute = changeTime(getminute);
var getSecond = changeTime(getsecond);
var gethours;
if(gethour>12){
gethours = "下午" + (gethour-12).toString();//changeTime(gethour-12)
} else {
gethours = "上午" + gethour.toString();//changeTime(gethour)
}
switch (format){
case "yyyy-MM-dd HH:mm:ss" : return getyear + "-" + getMonth + "-" + getDay + " " + gethour + ":" + getMinute + ":" + getSecond; break;
case "yyyy-MM-dd HH:m:s" : return getyear + "-" + getMonth + "-" + getDay + " " + gethour + ":" + getminute + ":" + getsecond; break;
case "yyyy-MM-dd hh:mm:ss" : return getyear + "-" + getMonth + "-" + getDay + " " + gethours + ":" + getMinute + ":" + getSecond; break;
case "yyyy-MM-dd hh:m:s" : return getyear + "-" + getMonth + "-" + getDay + " " + gethours + ":" + getminute + ":" + getsecond; break;
case "yyyy-M-d HH:mm:ss" : return getyear + "-" + getmonth + "-" + getday + " " + gethour + ":" + getMinute + ":" + getSecond; break;
case "yyyy-M-d HH:m:s" : return getyear + "-" + getmonth + "-" + getday + " " + gethour + ":" + getminute + ":" + getsecond; break;
case "yyyy-M-d hh:mm:ss" : return getyear + "-" + getmonth + "-" + getday + " " + gethours + ":" + getMinute + ":" + getSecond; break;
case "yyyy-M-d hh:m:s" : return getyear + "-" + getmonth + "-" + getday + " " + gethours + ":" + getminute + ":" + getsecond; break;
case "yyyy/MM/dd HH:mm:ss" : return getyear + "/" + getMonth + "/" + getDay + " " + gethour + ":" + getMinute + ":" + getSecond; break;
case "yyyy/MM/dd HH:m:s" : return getyear + "/" + getMonth + "/" + getDay + " " + gethour + ":" + getminute + ":" + getsecond; break;
case "yyyy/MM/dd hh:mm:ss" : return getyear + "/" + getMonth + "/" + getDay + " " + gethours + ":" + getMinute + ":" + getSecond; break;
case "yyyy/MM/dd hh:m:s" : return getyear + "/" + getMonth + "/" + getDay + " " + gethours + ":" + getminute + ":" + getsecond; break;
case "yyyy/M/d HH:mm:ss" : return getyear + "/" + getmonth + "/" + getday + " " + gethour + ":" + getMinute + ":" + getSecond; break;
case "yyyy/M/d HH:m:s" : return getyear + "/" + getmonth + "/" + getday + " " + gethour + ":" + getminute + ":" + getsecond; break;
case "yyyy/M/d hh:mm:ss" : return getyear + "/" + getmonth + "/" + getday + " " + gethours + ":" + getMinute + ":" + getSecond; break;
case "yyyy/M/d hh:m:s" : return getyear + "/" + getmonth + "/" + getday + " " + gethours + ":" + getminute + ":" + getsecond; break;
case "yyyy/MM/dd": return getyear + "/" + getMonth + "/" + getDay; break;
case "yyyy/M/d": return getyear + "/" + getmonth + "/" + getday ; break;
case "yyyy-MM-dd": return getyear + "-" + getMonth + "-" + getDay; break;
case "yyyy-M-d": return getyear + "-" + getmonth + "-" + getday; break;
}
}
/**
* 判断是否需要在时间前面加“0”
* @param time:需要处理的时间数字
* @returns {*}
*/
function changeTime(time) {
if(time<10){
time = "0" + time;
}
return time ;
}
部分效果展示
结束语
大家可以根据自己需要传参实现不同的时间格式化,也可以在此方法的基础上,进行自定义。