How to understand ES6 Date objects and object creation
本篇文章给大家带来的内容是关于如何理解ES6 Date对象以及对象的创建,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
ES6 Date对象详解
Date对象详解,创建对象
let date = new Date();
1.基本方法调用
年月日,时分秒,毫秒的基本方法的调用,getYear 和getFullYear的区别
//getYear获取的时间如果小于1900,那就要加上1900 //比如 2017 ,getYear获取的时间就是117,加上1900就是2017 var myYears = ( date.getYear() < 1900 ) ? ( 1900 + date.getYear() ) : date.getYear(); //getFullYear获取的就是当前系统本地的年 let year = date.getFullYear(); //由于js的月份是从0开始的,所以月份加上1 let month = date.getMonth()+1; //返回的是一个月中的某一天1-31 let myDate = date.getDate(); //返回的是一个星期中的某一天0-6,0是一个星期的第一天星期天 let myDay = date.getDay(); //获取24小时格式的小时 let hours = date.getHours(); //分 let minutes = date.getMinutes(); //秒 let seconds = date.getSeconds(); //当前时间的毫秒(0-999),获取更精确的时间 let milliseconds = date.getMilliseconds();
2.获取毫秒数的三种方式
//获取1970到现在的毫秒数 let time = date.getTime(); //返回Date对象的原始值的毫秒数, //返回值和方法 Date.getTime 返回的值相等。 let valueOfTime = date.valueOf(); //parse() 方法可解析一个日期时间字符串, //并返回 1970/1/1 午夜距离该日期时间的毫秒数。 //这个毫秒数是把当前毫秒变成000的毫秒数 let parseTime = Date.parse(date.toString()); //返回本地时间与格林威治标准时间 (GMT) 的分钟差,了解一下 let timezoneOffset = date.getTimezoneOffset();
3.获取Date对象字符串和本地时间字符串
//Date 对象,日期字符串 console.log(date.toDateString()); //Date 对象,时间字符串 console.log(date.toTimeString()); //Date 对象,日期+时间字符串 console.log(date.toString()); //日期字符串,根据本地时间格式 console.log(date.toLocaleDateString()); //时间字符串,根据本地时间格式 console.log(date.toLocaleTimeString()); //日期+时间字符串,根据本地时间格式 console.log(date.toLocaleString());
4.当前毫秒数转化为时分秒
//当前毫秒数转化为时分秒 let timeToDate = new Date(1487590667000).toLocaleString(); console.log(timeToDate); console.log(timeToDate.split("/").join('-'));
5.对Date的扩展,将 Date 转化为指定格式的String
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt) { //author: meizz var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; 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(date.Format('yyyy-MM-dd hh:mm:ss.S q')); console.log(date.Format('yyyy-M-d h:m:s.S q'))
The above is the detailed content of How to understand ES6 Date objects and object creation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

I encountered a problem. After springboot was upgraded to 2.0, the date found from the database was received using Date and finally returned directly to the front end. It can be displayed normally in the yyyy-MM-ddHH:mm:ss format in Google Chrome. However, the date displayed in the IE browser is "garbled" because the springboot1.x version returns a timestamp in the Date field by default, and Google and IE will automatically convert the timestamp into yyyy-MM-ddHH:mm:ss ;After springboot2.0, spring will automatically convert the Date field into a UTC string (without configuration), so date needs to be converted into a timestamp or y

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr

The java.util package provides the Date class to encapsulate the current date and time. The Date class provides two constructors to instantiate Date objects. The first constructor initializes the object with the current date and time: Date() The second constructor receives a parameter, which is the number of milliseconds since January 1, 1970. After the Date object is created, you can call the following method: Serial number method description 1booleanafter(Datedate), if the Date object calling this method returns true after the specified date, otherwise it returns false2booleanbefore(Datedate), if
