JS日期格式转换,字符串格式和Unix时间转换


分类:JS 作者:myerob 标签:JS 日期 格式 转换 字符串 Unix 时间转换 阅读:333 收藏
导读:
1.js将RFC3339时间格式转换为正常格式:yyyy-MM-dd hh:mm:ssvar time = '2021-07-10T13:15:14.7666463Z'; var date = ne…

(1)给出标准字符串时间转成unix机器时间

var stdStrTime = "2004-05-25T00:00:00.000Z";
var mTime = Date.parse(humanTime);
console.log(machineTime);
// output: 1085443200000

(2)给出unix机器时间转成标准字符串时间

function pad0(m){return m<10?'0'+m:m }
function format(unix)
{
//unix是整数,否则要parseInt转换
var time = new Date(unix); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+pad(m)+'-'+pad0(d)+' '+pad0(h)+':'+pad0(mm)+':'+pad0(s);
}

(3)js将RFC3339时间格式转换为正常格式:yyyy-MM-dd hh:mm:ss

var time = '2021-07-10T13:15:14.7666463Z';
var date = new Date(time).toJSON();
var timeft=new Date(+new Date(date)).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');

echarts.format.formatTime("yyyy-MM-dd hh:mm:ss",timeft);

(4)js将unix时间格式转换为正常格式:yyyy-MM-dd hh:mm:ss

var time = 1625922914766; // 毫秒
echarts.format.formatTime("yyyy-MM-dd hh:mm:ss", new Date(time)-8*3600*1000);// 往前减8个小时

(4)日期比较

var str1 = '2021-07-10 00:00:00';
var str2 = '2021-07-10 10:00:00';
var time1 = new Date(curTime);
var time2 = new Date(time);
if (time1.getTime() < time2.getTime()) {
   console.log("str1小于str2");
}

JS 中各种时间格式的示例:

时间格式 示例
中国标准时间Fri Mar 18 2022 14:24:45 GMT+0800(中国标准时间)
部分可读字符串Fri Mar 18 2022
格林威治时间Fri,18 Mar 2022 06:24:45 GMT
现在用的时间标准UTCFri Mar 18 2022 06:24:45 GMT
IOS标准时间(JSON时间格式)2022-03-18T06:24:45.061Z
常见时间格式2022-03-18 14:24:45

获取时间示例代码:

	        var newDate = new Date();
			
			console.log('newDate',newDate)
			
			//Fri Mar 18 2022 14:24:45 GMT+0800(中国标准时间)
			
			1.把 Date 对象的日期部分转换为可读字符串:
			
			console.log('toDateString',newDate.toDateString());
			
			// Fri Mar 18 2022
			
			2.根据格林威治时间 (GMT) 把 Date 对象转换为字符串(需注意的是)
			
			console.log('toGMTString',newDate.toGMTString());
			
			// Fri,18 Mar 2022 06:24:45 GMT
			
			不建议使用,因为此日期会在转换为字符串之前由本地时区转换为 GMT 时区。
			
			例如:传入具体时间
			
			var localDate=new Date('July 21, 1983 01:15:00')
			
			console.log(localDate.toGMTString())
			
			// Wed, 20 Jul 1983 17:15:00 GMT
			
			输出的时间会和传入的时间有时差
			
			3.使用 ISO 标准返回 Date 对象的字符串格式:
			
			console.log('toISOString',newDate.toISOString());
			
			// 2022-03-18T06:24:45.061Z
			
			
			4.将 Date 对象转换为字符串,并格式化为 JSON 数据格式。
			
			console.log('toJSON',newDate.toJSON());
			
			// 2022-03-18T06:24:45.061Z
			
			5.根据本地时间把 Date 对象的日期部分转换为字符串:
			
			console.log('toLocaleDateString',newDate.toLocaleDateString());
			
			// 2022/3/18
			
			6.根据本地时间把 Date 对象转换为字符串:
			
			console.log('toLocaleString',newDate.toLocaleString());
			
			// 2022/3/18 下午2:24:45
			
			7.根据本地时间把 Date 对象的时间部分转换为字符串:
			
			console.log('toLocaleTimeString',newDate.toLocaleTimeString());
			
			// 下午2:24:45
			
			8. Date 对象转换为字符串。
			
			console.log('toString',newDate.toString());
			
			// Fri Mar 18 2022 14:24:45 GMT+0800(中国标准时间)
			
			9.把 Date 对象的时间部分转换为字符串:
			
			console.log('toTimeString',newDate.toTimeString());
			
			// 14:24:45 GMT+0800(中国标准时间)
			
			10.根据世界时 (UTC) 把 Date 对象转换为字符串:
			
			console.log('toUTCString',newDate.toUTCString());
			
			//  Fri Mar 18 2022 06:24:45 GMT

日期转时间戳示例代码:

var date = new Date(); // 当前时间
console.log('date',date)

三种方法:
var time1 = date.getTime();
var time2 = date.valueOf();
var time3 = Date.parse(date);

console.log('time1',time1)
console.log('time2',time2)
console.log('time3',time3)   

注意:如果是用JSON格式的时间去转换时间戳的话,获取的时间有相差

var date =new Date('2022-03-16T16:00:00.000Z'); // 当前时间
		console.log('date',date)
		var time1 = date.getTime();
		var time2 = date.valueOf();
		var time3 = Date.parse(date);
		console.log('time1',time1)
		console.log('time2',time2)
		console.log('time3',time3)

   时间戳转日期示例代码:

function pad0(m){return m<10?'0'+m:m }
function format(unix)
{
//unix是整数,否则要parseInt转换
var time = new Date(unix); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+pad(m)+'-'+pad0(d)+' '+pad0(h)+':'+pad0(mm)+':'+pad0(s);
}

   注意:如果是‘2022-03-16T16:00:00.000Z’这种时间格式的话,上面那种方式获取的时间有误。可以试试下面这种方法:

 var time = '2022-03-16T16:00:00.000Z'
      time = time.replace(/-/, '年')
      time = time.replace(/-/, '月')
      time = time.replace(/T/, '日')
      let times = time.split('.')
      console.log(times[0])
      //2022年03月16日16:00:00

 


相关推荐