当前位置:网站首页>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>
边栏推荐
- [Halcon vision] software programming ideas
- 【杂谈】Error loading psycopg2 module :No module named psycopg2
- Wechat official account release reminder (wechat official account template message interface)
- Learning about opencv (3)
- 简单化构造函数的继承方法(二)- ES6中的class继承
- 关于函数模板描述错误的是(链接格式错误怎么解决)
- 详细解析js中的混合方式构造对象(构造加属性,原型加方法)
- [Halcon vision] image filtering
- Application of crosstab in SQL Server
- 句句解析js中的完美 / 缓冲运动框架(新手专用)
猜你喜欢
【Halcon视觉】图像的傅里叶变换
About automatic operation on Web pages
Review of database -- 1. Overview
Jpg to EPS
【Halcon视觉】数组
How to write a million reading article
如何写一篇百万阅读量的文章
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
Uniapp error 7 < Map >: marker ID should be a number
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
随机推荐
Session based recommendations with recurrent neural networks
Data communication foundation STP principle
【杂谈】Error loading psycopg2 module :No module named psycopg2
Force deduction DFS
Rocky basic exercise -shell script 2
Closure of go (cumulative sum)
畅听,网文流量竞争的下一站?
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
30分钟彻底弄懂 synchronized 锁升级过程
Solution of inputting whole line string after inputting integer
Jpg to EPS
The fourth week of summer vacation
The reason why go language is particularly slow to develop run and build commands
The CLOB field cannot be converted when querying Damon database
Structure of [Halcon vision] operator
[qualcomm][network] QTI service analysis
PLC概述
【Halcon视觉】数组
Flask框架初学-04-flask蓝图及代码抽离
数据库的复习--3.SQL语言