Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 日期转时间戳

作者:木の兮   发布日期:2026-03-23   浏览:90

// 将 JavaScript 日期对象转换为时间戳

// 创建一个 Date 对象,表示当前日期和时间
const date = new Date();

// 使用 getTime() 方法将日期对象转换为时间戳(以毫秒为单位)
const timestamp = date.getTime();

console.log("当前时间的时间戳(毫秒):", timestamp);

// 如果需要获取秒级时间戳,可以除以 1000 并取整
const timestampInSeconds = Math.floor(timestamp / 1000);

console.log("当前时间的时间戳(秒):", timestampInSeconds);

解释说明:

  • new Date():创建一个新的 Date 对象,表示当前的日期和时间。
  • getTime():返回自 1970 年 1 月 1 日 00:00:00 UTC 到当前日期对象所表示的时间之间的毫秒数。
  • Math.floor(timestamp / 1000):将毫秒级时间戳转换为秒级时间戳。

上一篇:js 时间戳转成日期

下一篇:js 时间戳转日期

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站