客户端,我在请求头拿到的时间格式是这样的“Mon, 06 Aug 2018 15:09:39 GMT”,我想转换成2018:01:02 12:12:12,这种格式,
我试了好几种方法,new date(Mon, 06 Aug 2018 15:09:39 GMT),这种拿到的是客户端时间,用js或jquery应该怎么转换格式
var format = function(time, format)
{
var t = new Date(time);
var tf = function(i){return (i < 10 ? ‘0’ : ”) + i};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
switch(a){
case ‘yyyy':
return tf(t.getFullYear());
break;
case ‘MM':
return tf(t.getMonth() + 1);
break;
case ‘mm':
return tf(t.getMinutes());
break;
case ‘dd':
return tf(t.getDate());
break;
case ‘HH':
return tf(t.getHours());
break;
case ‘ss':
return tf(t.getSeconds());
break;
}
})
}
alert(format(“Thu Aug 22 2013 15:12:00 GMT+0800″, ‘yyyy-MM-dd HH:mm:ss’));
**
* @param datdString Thu May 18 2017 00:00:00 GMT+0800 (中国标准时间)
* @return 年月日;
*/
public static String parseTime(String datdString) {
datdString = datdString.replace("GMT", "").replaceAll("\\(.*\\)", "");
//将字符串转化为date类型,格式2016-10-12
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
Date dateTrans = null;
try {
dateTrans = format.parse(datdString);
return new SimpleDateFormat("yyyy-MM-dd").format(dateTrans).replace("-","/");
} catch (ParseException e) {
e.printStackTrace();
}
return datdString;
}
/**
* @param datdString "Tue Jul 12 12:10:11 GMT+08:00 2016";
* @return 时分秒
*/
public static String parseHour(String datdString) {
datdString = datdString.replace("GMT", "").replaceAll("\\(.*\\)", "");
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
Date dateTrans = null;
try {
dateTrans = format.parse(datdString);
return new SimpleDateFormat("HH:mm:ss").format(dateTrans);
} catch (ParseException e) {
e.printStackTrace();
}
return datdString;
}
//获取Date变量然后用一个SimpleDateFormat时间转换格式类 如Date date=new Date()
Date date =new Date();
SimpleDateFormat sdf=new SimpleDateFormat ("yyyy-MM-dd");
System.out.println(sdf.format(date));
Date.prototype.format = function (format) {
var formatWeek = function(day) {
switch (day) {
case 0:
return '周日';
case 1:
return '周一';
case 2:
return '周二';
case 3:
return '周三';
case 4:
return '周四';
case 5:
return '周五';
case 6:
return '周六';
}
};
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),
"w+": formatWeek(this.getDay()),
"S": this.getMilliseconds()
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
new Date().format('yyyy-MM-dd HH:mm:ss')