当前位置:网站首页>Time and date processing in JS
Time and date processing in JS
2022-06-13 08:30:00 【SwJieJie】
1, Common methods of processing date :
var myDate = new Date();
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
2,js Get current date , And format it as YYYY-MM-DD
(1), Method 1 : Common code to handle
// Get current date directly
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
perhaps
// Get the current date according to the timestamp
function transformationDate (date) {
const Month = date.getMonth() + 1
return `${
date.getFullYear()}-${
Month > 9 ? Month : `0${
Month}`}-${
date.getDate() > 9 ? date.getDate() : `0${
date.getDate()}`}`
}
function getLocalTime(nS) {
var d = new Date(parseInt(nS)* 1000); // Time objects generated from timestamps
var date = (d.getFullYear()) + "-" +
(d.getMonth() + 1) + "-" +
(d.getDate()) + " " +
(d.getHours()) + ":" +
(d.getMinutes()) + ":" +
(d.getSeconds());
return date;
}
document.write(getLocalTime(1642402680905));
console.log(transformationDate(new Date(1642402680905)))
// result : 2022-01-17
perhaps
// Get the current time and format "yyyy-MM-dd HH:mm:ss"
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // month
"d+": this.getDate(), // Japan
"H+": this.getHours(), // Hours
"m+": this.getMinutes(), // branch
"s+": this.getSeconds(), // second
"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
"S": this.getMilliseconds() // millisecond
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
console.log(new Date().Format("yyyy-MM-dd"))
// result :2022-01-17
console.log(new Date().Format("yyyy-MM-dd HH:mm:ss"))
// result :2022-01-17 14:55:10
Method 2 : The use of plug-in moment.js To deal with
1, install moment.js The library of
npm install moment --save
moment().format('YYYY-MM-DD')
3, Convert date time string to timestamp
var date = '2015-03-05 17:59:00.0';
date = date.substring(0,19);
date = date.replace(/-/g,'/');
var timestamp = new Date(date).getTime();
document.write(timestamp);
4, Current time to timestamp
var timestamp = parseInt(new Date().getTime()/1000); // Current timestamp
document.write(timestamp);
5, Get the corresponding day of the week according to the timestamp
getWeek (time) {
var d = new Date(time).getDay()
const weekDay = [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday ']
return `(${
weekDay[d]})`
}
边栏推荐
- Buuctf web (III)
- Start from scratch - implement the jpetstore website -1- establish the project framework and project introduction
- Dest0g3 520迎新賽
- Remote access and control
- 从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
- Phpexcel 10008 error resolution
- d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
- Common shell script development specifications
- LVM management exercise
- 【leetcode周赛记录】第80场双周赛记录
猜你喜欢
JS - max. of array cases
How about a well-known food material distribution information management system?
将solidworks建的机器人模型导入到ros中
Redis subscribe connection timeout interrupt problem solution
Is there any good management software to solve the problems faced by tea wholesalers
Disk management and disk partition operation
Phpexcel 10008 error resolution
CCNP_ BT-MGRE
从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
Buuctf web (IV)
随机推荐
How does the BD new tab plug-in log in?
Determine whether a string is rotated from another string
微服务项目搭建三:自动生成代码
情绪处理技巧
Dest0g3 520迎新賽
Do not update the sub component page of parameter object passed from parent to child of nailing applet?
What software can be used to solve the problems faced by the auto parts industry
Tiktok keyword search list interface, ultra detailed interface docking steps
Gtk+ programming example on page 115 - simplest progress bar 2 with steps to write GTK program using anjuta
CCNP_ BT static routing
How to use annotations in word
Dfinity (ICP) development problems and solutions-6
When submitting the laravel admin form and using the required verification, an error is reported when the value is 0
SQL injection experiment
Dest0g3 520 orientation
Numeric types in SQL. Try to avoid using null as the default value
Did decentralized digital identity
JS wrapper object
JS - simple ATM of the for cycle case
redis.exceptions.ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.