Convert timestamp to year, month, day, hour, minute, second

1. Use new Date() to convert timestamp to time

var date = new Date(time);

2. Extract year

var Y = date.getFullYear() + '-';

3. Extract the month to judge whether it is necessary to add 0

var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';

4. Get the date to judge whether it is necessary to add 0

var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';

5. Judging whether it is necessary to add 0 when obtaining

var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';

6. Obtain points to judge whether it is necessary to add 0

var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());

7. Get seconds to judge whether it is necessary to add 0

var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());

full code

time(time){
			var date = new Date(time);
			var Y = date.getFullYear() + '-'; 
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
			var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
			var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
			var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
			var time1 = Y + M + D + h + m;
		}

Guess you like

Origin blog.csdn.net/weixin_61964186/article/details/129620558
Recommended