当前位置:网站首页>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>
边栏推荐
- SPARK中 DS V2 push down(下推)的一些说明
- Basic usage of protobuf
- [Qualcomm][Network] qti服务分析
- Common errors when starting projects in uniapp ---appid
- Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
- 【Halcon视觉】软件编程思路
- 软件打不开了
- Mlx90640 infrared thermal imager temperature sensor module development notes (6)
- How to use Gmail to pick up / send mail on Foxmail
- 数通基础-TCPIP参考模型
猜你喜欢
[qualcomm][network] QTI service analysis
Flask框架初学-03-模板
单元测试,到底什么是单元测试,为什么单测这么难写
数据库的复习--3.SQL语言
我们的Web3创业项目,黄了
【Halcon视觉】形态学膨胀
Review of database -- 1. Overview
[Halcon vision] array
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
数通基础-二层交换原理
随机推荐
In Net 6.0
Strange Towers of Hanoi|汉诺塔4柱问题
Getting started with SQL - combined tables
Use of pclint in vs2013
函数模板与同名的非模板函数不可以重载(重载的定义)
【Halcon视觉】软件编程思路
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
简单化构造函数的继承方法(一)- 组合继承
句句解析js中的完美 / 缓冲运动框架(新手专用)
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
[C language] LINQ overview
[C language] named type and anonymous type
modelsim 安装教程(应用未安装)
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
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
About the declaration and definition of template functions [easy to understand]
The problem of incomplete or partial display of the last recyclerview is solved
Function templates and non template functions with the same name cannot be overloaded (definition of overloads)
Redis realizes the correct posture of token bucket
面试第二家公司的面试题及答案(二)