当前位置:网站首页>JS get the current time, time and timestamp conversion
JS get the current time, time and timestamp conversion
2022-07-26 13:41:00 【Tangce】
One 、 attribute
11.6 Get the current time , count down
js Get time from new date() Usage of
var myDate = new Date();// Get the current time of the system
myDate.getYear(); // Get current year (2 position )
myDate.getFullYear(); // Get full year (4 position ,1970-???)
myDate.getMonth(); // Get current month (0-11,0 representative 1 month )
myDate.getDate(); // Get current day (1-31)
myDate.getDay(); // Get current week X(0-6,0 For Sunday )
myDate.getTime(); // Get the current time ( from 1970.1.1 Milliseconds to start )
myDate.getHours(); // Get current hours (0-23)
myDate.getMinutes(); // Get current minutes (0-59)
myDate.getSeconds(); // Get current seconds (0-59)
myDate.getMilliseconds(); // Get current milliseconds (0-999)
myDate.toLocaleDateString(); // Get current date
var mytime=myDate.toLocaleTimeString(); // Get the current time
myDate.toLocaleString( ); // Get date and time
Two 、 Example
Eg:
One : Time to time stamp :javascript There are four ways to get a timestamp , All by instantiating time objects new Date() To further obtain the current timestamp
1.var timestamp1 = Date.parse(new Date()); // result :1477808630000 This method is not recommended , Millisecond level values are converted to 000
console.log(timestamp1);
2.var timestamp2 = (new Date()).valueOf(); // result :1477808630404 adopt valueOf() Function returns the original value of the specified object to get the exact timestamp value
console.log(timestamp2);
3.var timestamp3 = new Date().getTime(); // result :1477808630404 , Get the millisecond value of the current time directly through the prototype method , accuracy
console.log(timestamp3);
4.var timetamp4 = Number(new Date()) ; // result :1477808630404 , Turn time into a number Type value , Timestamp
console.log(timetamp4);
3、 ... and 、demo
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Easy to use countdown js Code - Stationmaster material </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 God </span>
<span id="t_h">00 when </span>
<span id="t_m">00 branch </span>
<span id="t_s">00 second </span>
</div>
<script>
function GetRTime(){
// Double 11 countdown draw
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 + " God ";
document.getElementById("t_h").innerHTML = h + " when ";
document.getElementById("t_m").innerHTML = m + " branch ";
document.getElementById("t_s").innerHTML = s + " second ";
}
setInterval(GetRTime,0);
</script>
</body>
</html>
边栏推荐
- 消息的订阅和发布
- Time complexity analysis of bubble sorting
- Unicode文件解析方法及存在问题
- B+ tree index use (9) grouping, back to table, overlay index (21)
- Concept and handling of exceptions
- 估值15亿美元的独角兽被爆裁员,又一赛道遇冷?
- Pytorch学习笔记(一)安装与常用函数的使用
- Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
- Comparator (interface between comparable and comparator)
- Multi objective optimization series 1 --- explanation of non dominated sorting function of NSGA2
猜你喜欢

Ultimate doll 2.0 | cloud native delivery package

估值15亿美元的独角兽被爆裁员,又一赛道遇冷?

Pytorch学习笔记(一)安装与常用函数的使用

银行业客户体验管理现状与优化策略分析

HCIP第十一天比较(BGP的配置、发布)

File upload and download performance test based on the locust framework

飞盘,2022年“黑红”顶流

HCIP第十二天笔记整理(BGP联邦、选路规则)

The shell script in Jenkins fails to execute but does not exit by itself
![[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp](/img/d4/7b9c7c99d46661e1be2963a342dd18.jpg)
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp
随机推荐
Pytorch学习笔记(三)模型的使用、修改、训练(CPU/GPU)及验证
Sword finger offer (x): rectangular coverage
B+ tree index uses (7) matching column prefix, matching value range (19)
带你熟悉云网络的“电话簿”:DNS
华为机考 ~ 偏移量实现字符串加密
LCL三相pwm整流器(逆变器)
AI theory knowledge map 1 Foundation
Concept and handling of exceptions
Subscription and publication of messages
Feixin, which lasted 15 years and had 500million users, was completely dead
LeetCode 217. 存在重复元素
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
[oauth2] v. oauth2loginauthenticationfilter
B+ tree (3) clustered index, secondary index -- MySQL from entry to proficiency (XV)
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]
TDSQL-C Serverless:助力初创企业实现降本增效
LeetCode 1523. 在区间范围内统计奇数数目
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
One stroke problem (Chinese postman problem)