-

时间戳转换(图文教程)

时间戳是记录时间的数字标识符,用于标识事件发生的时间点。
js版时间戳转换
生成13位时间戳及解析生成时间 尝试一下>>
生成10位时间戳及解析生成时间 尝试一下>>
var date = new Date();  
var milliseconds = date.getTime();  

var now=date.getFullYear()+'/'+date.getMonth() + 1+'/'+date.getDate()+' '+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();  
document.write("当前时间date="+now)
document.write("<br>时间戳milliseconds="+milliseconds)  

// 时间戳  
var timestamp = 1702695452073;    
// 创建一个新的Date对象  
var date = new Date(timestamp);  
  
// 格式化日期   
document.write("<br><hr><br>时间戳timestamp="+timestamp) 
var sTime=date.getFullYear()+'/'+ (date.getMonth() + 1) +'/'+date.getDate()+' '+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
document.write("<br>格式化日期date="+sTime)  
        

生成10位时间戳及解析生成时间
//生成10位时间戳
function generateTimestamp() {  
  const date = new Date();  
  const timestamp = date.getTime();  
  return timestamp.toString().substr(0, 10);  
}   

//解析10位时间戳
function parseTimestamp(timestamp) {  
  const date = new Date(timestamp * 1000); // 将时间戳转换为毫秒数  
  const year = date.getFullYear();  
  const month = date.getMonth() + 1; // 月份从0开始,所以需要加1  
  const day = date.getDate();  
  const hours = date.getHours();  
  const minutes = date.getMinutes();  
  const seconds = date.getSeconds();  
  
  return date.getFullYear()+'/'+ (date.getMonth() + 1)+'/'+date.getDate()+' '+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
}  




 
document.write("当前时间date="+ new Date())
document.write("
时间戳milliseconds="+generateTimestamp()) // 时间戳 var timestamp = 1702695452; document.write("


时间戳timestamp="+timestamp) // 格式化日期 document.write("
格式化日期date="+parseTimestamp(timestamp))
asp版时间戳转换
生成时间戳及解析生成时间 尝试一下>>
response.addheader "Content-Type", "text/html; charset=utf-8"   '删除BOM seblime > 文件 > 以什么编码保存 > utf-8 with BOM'


'把标准时间转换为UNIX时间戳 response.Write toUnixTime(now(), +8)
Function toUnixTime(strTime, intTimeZone)
    If IsEmpty(strTime) or Not IsDate(strTime) Then strTime = Now
    If IsEmpty(intTimeZone) or Not isNumeric(intTimeZone) Then intTimeZone = 0
    toUnixTime = DateAdd("h",-intTimeZone,strTime)
    toUnixTime = DateDiff("s","1970-01-01 00:00:00", toUnixTime)
End Function
'把UNIX时间戳转换为标准时间 response.Write "<hr>" & fromUnixTime("1513858770", +8)
Function fromUnixTime(intTime, intTimeZone)
    If IsEmpty(intTime) or Not IsNumeric(intTime) Then
        fromUnixTime = Now()
        Exit Function
    End If         
    If IsEmpty(intTime) or Not IsNumeric(intTimeZone) Then intTimeZone = 0
    fromUnixTime = DateAdd("s", intTime, "1970-01-01 00:00:00")
    fromUnixTime = DateAdd("h", intTimeZone, fromUnixTime)
End Function

'把标准时间转换为13位UNIX时间戳 20231216
Function to13UnixTime(strTime)
    to13UnixTime = (strTime - CDate("01/01/1970 00:00:00")) * 86400000 
End Function

'把13位UNIX时间戳转换为标准时间 
Function from13UnixTime(intTime)
    from13UnixTime=DateAdd("s", CLng(intTime / 1000), "01/01/1970 00:00:00")
End Function

response.write("现在时间:" & now() & "<br>")
response.write("10位时间戳:" & toUnixTime(now(),+8) & "<br>")



response.write("<hr><br>10位时间戳:1702695452<br>")
response.write("格式化时间:" & fromUnixTime("1702695452", +8) & "<br>")


response.write("<hr><br>13位时间戳:" & to13UnixTime(now()) & "<br>") 


response.write("<hr><br>10位时间戳:1702695452073<br>")
response.write("格式化时间:" & from13UnixTime("1702695452073") & "<br>")

php版时间戳转换
生成时间戳及解析生成时间 尝试一下>>
$current_time = date("Y-m-d H:i:s");
echo "当前时间: " . $current_time . "≪tbr>";


echo "当前时间戳: " . time()."≪tbr>";  



$current_time = time();  
$timestamp13 = $current_time * 1000;  
echo "当前13位时间戳: " . $timestamp13;  

echo "≪tbr>≪thr>≪tbr>";

$timestamp = 1633022400; // 示例时间戳
$time = date("Y-m-d H:i:s", $timestamp); // 将时间戳转换为时间
echo $timestamp." 10位时间戳转换为时间: " . $time; 
echo "≪tbr>";

 

 
$timestamp13 = 1626720000000; // 假设这是你的13位时间戳   
// 将13位时间戳转换为时间  
$date = date('Y-m-d H:i:s', $timestamp13 / 1000);  
  
echo $timestamp13." 13位转换后的时间: " . $date;