当前位置:网站首页>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]})`
}
边栏推荐
- Using KVM to create three virtual machines that can communicate with local area network
- Mongodb test case
- Shell脚本常用开发规范
- [game theory complete information static game] Application of Nash equilibrium
- Create a substrate private network
- Three methods to make the scroll bar of div automatically scroll to the bottom
- Custom exception class myexception
- CCNP_ Summary (Continued)
- [game theory complete information static game] Nash equilibrium
- Is there any good management software to solve the problems faced by tea wholesalers
猜你喜欢

What software can be used to solve the problems faced by the auto parts industry

使用kvm创建三台能通局域网的虚拟机

CCNP_ Bt- Reissue

Is there any good management software to solve the problems faced by tea wholesalers

How app inventor accesses resource files in assets directory

Introduction to dfinity (ICP) -1

Gtk+ programming example on page 115 - simplest progress bar 2 with steps to write GTK program using anjuta

HCIP_ MGRE comprehensive experiment

Buuctf web (VI)

Live broadcast review | bas technology innovation exploration under active defense system
随机推荐
MySQL query exercise
How to dynamically delete data rows in a table through JS (keep the head)
CCNP_ Bt-ospf big experiment (1)
Mysql_ Preliminary summary of database data (Continued)
Leetcode- sort arrays by parity
File upload question type
anaconda下安装pytorch
抖音关键词搜索列表接口,超详细的接口对接步骤
LVM management exercise
[game theory complete information static game] Nash equilibrium
ERP basic data Huaxia
杨氏矩阵查找数字是否存在
Buuctf web (VI)
DNS domain name resolution service
Taobao commodity sales interface / Taobao commodity sales monitoring interface / cumulative commodity sales interface
第115页的gtk+编程例子——最简单的进度条2附带使用Anjuta写gtk程序的步骤
Determine whether a string is rotated from another string
MySQL installation and configuration under Windows
Edge browser uses bdtab new tab plug-in (BD new tab)
HCIP_ Static experiment