当前位置:网站首页>js 获得当前时间,时间与时间戳的转换
js 获得当前时间,时间与时间戳的转换
2022-07-26 10:24:00 【唐策】
一、属性
11.6获得当前时间,倒计时
js中获取时间new date()的用法
var myDate = new Date();//获取系统当前时间
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-???)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
二、例子
Eg:
一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳
1.var timestamp1 = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000
console.log(timestamp1);
2.var timestamp2 = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值
console.log(timestamp2);
3.var timestamp3 = new Date().getTime(); // 结果:1477808630404 ,通过原型方法直接获得当前时间的毫秒值,准确
console.log(timestamp3);
4.var timetamp4 = Number(new Date()) ; //结果:1477808630404 ,将时间转化为一个number类型的数值,即时间戳
console.log(timetamp4);
三、demo
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>简单易用的倒计时js代码 - 站长素材</title>
<style>
*{ margin:0; padding:0; list-style:none;}
body{ font-size:18px; text-align:center;}
.time{ height:30px; padding:200px;}
</style>
</head>
<body>
<div class="time">
<span id="t_d">00天</span>
<span id="t_h">00时</span>
<span id="t_m">00分</span>
<span id="t_s">00秒</span>
</div>
<script>
function GetRTime(){
//双十一倒计时抽奖
var EndTime= new Date('2018/11/11 00:00:00');
var NowTime = new Date();
var t =EndTime.getTime() - NowTime.getTime();
var d=Math.floor(t/1000/60/60/24);
var h=Math.floor(t/1000/60/60%24);
var m=Math.floor(t/1000/60%60);
var s=Math.floor(t/1000%60);
document.getElementById("t_d").innerHTML = d + "天";
document.getElementById("t_h").innerHTML = h + "时";
document.getElementById("t_m").innerHTML = m + "分";
document.getElementById("t_s").innerHTML = s + "秒";
}
setInterval(GetRTime,0);
</script>
</body>
</html>
边栏推荐
- Tower of Hanoi II | tower of Hanoi 4 columns
- SQL优化的魅力!从 30248s 到 0.001s
- Okaleido ecological core equity Oka, all in fusion mining mode
- 单元测试,到底什么是单元测试,为什么单测这么难写
- Dynamically determine file types through links
- Learning about opencv (1)
- Mlx90640 infrared thermal imager temperature sensor module development notes (6)
- 【Halcon视觉】图像灰度变化
- Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
- Session based recommendations with recurrent neural networks
猜你喜欢

Flask框架初学-04-flask蓝图及代码抽离
![Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da](/img/21/5dceab9815b503f0b62d26a430d7eb.png)
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da

Meeting OA project (III) -- my meeting (meeting seating and submission for approval)

SQL优化的魅力!从 30248s 到 0.001s

我们的Web3创业项目,黄了

Dynamically determine file types through links

How to write a million reading article

单元测试,到底什么是单元测试,为什么单测这么难写

INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution

【Halcon视觉】图像滤波
随机推荐
【Halcon视觉】图像滤波
Distributed network communication framework: how to publish local services into RPC services
[C language] LINQ overview
Vs2019 configuring opencv
【C#语言】具名类型和匿名类型
PLC概述
【Halcon视觉】图像灰度变化
RecyclerView最后一条显示不全或显示部分的问题解决
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
【Halcon视觉】形态学膨胀
Review of database -- 3. SQL language
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Data communication foundation - layer 2 switching principle
【Halcon视觉】编程逻辑
3.1 leetcode daily question 6
新建福厦铁路全线贯通 这将给福建沿海带来什么?
数通基础-TCPIP参考模型
Review of database -- 1. Overview
Netease cloud UI imitation -- & gt; sidebar
Like, "new programmer" e-book is free for a limited time!