jsunix时间戳转换成时间

jsunix时间戳转换成时间,js实现unix时间戳转换代码教程如下:

输入一个时间

实现结果:转换成时间戳

js实现代码如下:

/**
* @act 鏃堕棿鎴宠浆鎹�
* @version 1.1
* @author youngxj
* @date 2018-07-01
* @url http://www.youngxj.cn
*/

showTime();

function timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);//鏃堕棿鎴充负10浣嶉渶*1000锛屾椂闂存埑涓�13浣嶇殑璇濅笉闇€涔�1000
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
return Y+M+D+h+m+s;
}
function moment(config) {
this._d = new Date(config._d != null ? config._d.getTime() : NaN);
if (!this.isValid()) {
this._d = new Date(NaN);
}
if (updateInProgress === false) {
updateInProgress = true;
hooks.updateOffset(this);
updateInProgress = false;
}
}
function format(timestamp) {
var time = new Date(timestamp);
var year = time.getFullYear();
var month = (time.getMonth() + 1) > 9 && (time.getMonth() + 1) || ('0' + (time.getMonth() + 1))
var date = time.getDate() > 9 && time.getDate() || ('0' + time.getDate())
var hour = time.getHours() > 9 && time.getHours() || ('0' + time.getHours())
var minute = time.getMinutes() > 9 && time.getMinutes() || ('0' + time.getMinutes())
var second = time.getSeconds() > 9 && time.getSeconds() || ('0' + time.getSeconds())
var YmdHis = year + '-' + month + '-' + date
+ ' ' + hour + ':' + minute + ':' + second;
return YmdHis;
}
$('#btn_state_o').on('click', function (e) {
if ($('#o_timestamp').val() == "") {layer.alert('浣犳槸涓嶆槸蹇樿濉唴瀹逛簡锛�');return false;}
e.preventDefault();
var timestamp = $('#o_timestamp').val();
timestamp = timestamp.replace(/^\s+|\s+$/, '');
if (/^\d{10}$/.test(timestamp)) {
timestamp *= 1000;
} else if (/^\d{13}$/.test(timestamp)) {
timestamp = parseInt(timestamp);
} else {
layer.alert('鏃堕棿鎴充笉姝g‘');
return;
}
var YmdHis = format(timestamp);
$('#o_datetime').val(YmdHis);
});

$('#btn_state_f').on('click', function (e) {
if ($('#mirror_field').val() == "") {layer.alert('浣犳槸涓嶆槸蹇樿濉唴瀹逛簡锛�');return false;}
e.preventDefault();
$('#f_timestamp').val(Date.parse($('#f_datetime').val()));
});
//瀹氫箟鍑芥暟锛氭瀯寤鸿鏄剧ず鐨勬椂闂存棩鏈熷瓧绗︿覆
function showTime()
{ //鍒涘缓Date瀵硅薄
var today = new Date(); //鍒嗗埆鍙栧嚭骞淬€佹湀銆佹棩銆佹椂銆佸垎銆佺
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds(); //濡傛灉鏄崟涓暟锛屽垯鍓嶉潰琛�0
month = month<10 ? "0"+month : month;
day = day <10 ? "0"+day : day;
hours = hours<10 ? "0"+hours : hours;
minutes = minutes<10 ? "0"+minutes : minutes;
seconds = seconds<10 ? "0"+seconds : seconds;
var str = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
var datetime = document.getElementById("js_datetime");
var timestamp = document.getElementById("js_timestamp");
datetime.innerHTML = str;
timestamp.innerHTML = Date.parse(str);
window.setTimeout("showTime()",1000);
}
function to_datetime(){
var text = $('#js_datetime').html();
$('#mirror_field').val(text);
$('#f_datetime').val(text);
}
function to_timestamp(){
var text = $('#js_timestamp').html();
$('#o_timestamp').val(text);
}

显示效果:unix时间戳转换

 参考文章:http://tishang.net/articles/1685778020.html

快乐下载

软件屋
铁盒下载

​​​​​​中元手游网

刷ip工具

端口扫描

二维码生成器

草料二维码生成器

网站测速工具

posted @ 2023-06-03 15:48  kaiser099  阅读(170)  评论(0编辑  收藏  举报