search
HomeWeb Front-endJS TutorialSummary of the use of Date objects in javascript

JSON date to JS date, we know that after the date type is converted to JSON, the returned data is similar to this:

  /Date(1379944571737)/

 But this kind of date cannot be displayed directly because no one knows what it is. Meaning, here is a way to convert JSON date to JS date.

function ConvertJSONDateToJSDate(jsondate) {
 var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
 return date;
}


provides two date formats for Date conversion:

//yyyy-MM-dd
function getDate(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 return year + "-" + month + "-" + day ;
}
//yyyy-MM-dd HH:mm:SS
function getDateTime(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 var hh = date.getHours();
 var mm = date.getMinutes();
 var ss = date.getSeconds();
 return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}


How to convert a string into a Date object:

var str = "2012-12-12";
var date = new Date(str);  //字符串转换为Date对象
document.write(date.getFullYear());  //然后就可以使用Date对象的方法输出年份了


1.Date .getDate() Returns the day of the month in the date object.

var date = new Date();  //2012-12-19
document.write(date.getDate());  //返回 19 是19号


2. Date.getDay() Returns the day of the week in the date Sunday 0-6

var date = new Date();
document.write(date.getDay());  //3 星期3


3. Date.getFulYead() Returns the year such as 2012.

var date = new Date();
document.write(date.getFullYear());  //返回2012,2012年


4. Date.getHours() Returns the hours in the date, what time it is, 0-23

var date = new Date();
document.write(date.getHours());  //返回23,晚上11点


5. Date.getMilliseconds() Returns the milliseconds in the date

var date = new Date();
document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒


6. Date.getMinutes()                     

 

                        vi. (January)-11(December)

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分

Eight. Date.getSeconds() //Returns a description of a date

var date = new Date();
document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

Nine. Date.getTime() //Will A date object returns

var date = new Date();
document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒


, date.gettimezoneoffset () // gmt time is different from the local time. / Return the date value in the Date object, (global time)

var date = new Date();
document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

12. Date.getUTCDay() // Return the day of the week in the Date object, (global time)

var date = new Date();
document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。

Thirteen. Date.getUTCFulYear() // Returns the year in Date, 4 digits, such as 2012, (global time)

var date = new Date();
document.write(date.getUTCDate());  //返回19  19号

Fourteen. Date.getUTCHours() // Returns the year in Date object The number of hours, that is, what time it is now, finally has one that is different from getHours(). It should be related to the time difference, and the returned value is in global time.

var date = new Date();
document.write(date.getUTCDay());  //返回3  星期3

Fifteen, Date.getUTCMillisconds() //Returns the number of milliseconds in the Date object, (global time)

var date = new Date();
document.write(date.getUTCFullYear());  //返回2012

Sixteen, Date.getUTCMinutes() //Returns D ate The number of minutes in the object, (global time)

var date = new Date();
document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。

Seventeen, Date.getUTCMonth() //Returns the month value in the Date object, (global time)

var date = new Date();
document.write(date.getMilliseconds());  //返回全球时间中的毫秒数

Eighteen, Date.getUTCSeconds()  //Returns the seconds value in the Date object

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。

Nineteen, Date.getYear()  //Returns the year value in the Date object minus 1900

var date = new Date();
document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  

20. Date.now() Static method // Returns the time interval from midnight on January 1, 1970 to now, expressed in milliseconds

var date = new Date();
document.write(date.getSeconds());  //返回秒数值 返回33

21. Date.parse() // Parse a date Time string, returns the number of milliseconds between midnight on January 1, 1970 and the given date

var date = new Date();
document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)

Twenty-two, Date.setDate() //Set the date value in a Date object, return value Expressed in milliseconds of the adjusted date

document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。

23. Date.setFullYear() // Set the year in a Date object, and the return value is expressed in milliseconds of the adjusted date.

var date = "2012-12-19";
document.write(Date.parse(date));  //返回  1355875200000
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //输出2012-11-19  //注意月份是从0-11

Twenty-four, Date.setHours() //Set the number of events in a Date object, and the return value is expressed in milliseconds of the adjusted date.

var date = new Date();
document.write(date.setDate(11));  //返回1355236647980    //设置为11,其实是12月,设置为3其实是4月
    var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意

Twenty-five, Date.setMilliseconds() //Set the number of milliseconds on a date

var date = new Date();  今天是2012-12-20
document.write(date.setFullYear(1989)); //返回630167981030
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出1989-11-20

Twenty-six, Date.setMinutes() //Set the number of minutes on a date

var date = new Date(); //现在是2012-12-52 22:52
document.write(date.setHours(5)); //返回1355954000882
var da = new Date(date);
document.write("<br/>" + da.getHours()); //输出05

Twenty-seven, Date.setMonth() //Set the number of months for a date

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(22)); //返回1356015393022    注意最后两位,无论如何刷新都是22

Twenty-eight, Date.setSeconds() //Set the description syntax of a date :date.setSeconds(seconds) Date.setSeconds(seconds,millis)

var date = new Date(); //现在是2012-12-52 22:52
document.write(date.setMinutes(1)); //返回1356012067105
var da = new Date(date);
document.write("<br/>" + da.getMinutes()); //输出1

Twenty-nine, Date.setTime() //Set a time using milliseconds Syntax: date.setTime(milliseonds)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(2)); //返回1332255597722
var da = new Date(date);
document.write("<br/>" + da.getMonth()); //输出2

Thirty, Date.setUTCDate() //Set the date value corresponding to the month in a Date object, which is the day (global time) Syntax: date.setUTCDate(day-of-month)

var date = new Date(); //现在是2012-12-20
document.write(date.setSeconds(3)); //返回1356015783872
var da = new Date(date);
document.write("<br/>" + da.getSeconds()); //输出3

三十一、Date.setUTCFullYear()     //设置一个Date对象中对应的年份,全球时间      语法:date.setUTCFullYear(year)         date.setUTCFullYear(year,month)         date.setUTCFullYear(year,month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCFullYear(1999)); //返回945702713666
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1999

   


三十二、Date.setUTCHours()      //设置一个Date对象中对应的小时数,(全球时间)      语法:date.setUTCHours(hours)          date.setUTCHours(hours,minutes)         date.setUTCHours(hours,minutes,seconds)         date.setUTCHours(hours,minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCHours(05)); //返回1355980581928
var da = new Date(date);
document.write("<br/>" + da.getUTCHours()); //输出5

   


三十三、Date.setUTCMilliseconds()  //设置一个Date对象中对应的毫秒数,(全球时间)     语法:date.setUTCMilliseconds(millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(05)); //返回1356016784005  注意此处无论如何刷新都是05结尾

   


三十四、Date.setUTCMinutes()    //设置一个Date对象的分钟、秒钟、以及毫秒值。     语法:date.setUTCMinutes(minutes)     date.setUTCMinutes(minutes,seconds)                date.setUTCMinutes(minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCMinutes(25)); //返回1356017146549
var da = new Date(date);
document.write("<br/>" + da.getUTCMinutes()); //输出5

   


三十五、Date.setUTCMonth()    //设置一个Date对象的月份值及日期值        语法:date.setUTCMonth(month)          date.setUTCMonth(month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(01)); //返回1329751527983
var da = new Date(date);
document.write("<br/>" + da.getUTCMonth()); //输出1

   


三十六、Date.setUTCSeconds()    //设置一个Date的秒钟及毫秒值      语法:date.setUTCSeconds(seconds)         date.setUTCSeconds(seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCSeconds(01)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getUTCSeconds()); //输出1

   


三十七、Date.setYears()      //设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数                    则把它当成FullYear设置      语法:date.setYears(year)

var date = new Date(); //现在是2012-12-20
document.write(date.setYear(22)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1922
var date = new Date(); //现在是2012-12-20
document.write(date.setYear(2011)); //返回1324395113386
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出2011

   


三十八、Date.toDateString()    //以字符串的形式返回一个Date的日期部分     语法:date.toDateString()

var date = new Date(); //现在是2012-12-20
document.write(date.toDateString("yyyy-MM-dd")); //返回Thu Dec 20 2012

   


三十九、Date.toTimeString()    //以字符串的形式返回一个Date的时间部分     语法:date.toTimeString()

var date = new Date(); //现在是2012-12-20
document.write(date.toTimeString("yyyy-MM-dd")); //返回23:38:33 GMT+0800

   


 四十、Date.toISOString()      //将一个Date对象转换为ISO-8601格式的字符串     语法;date.toISOString()  //返回的字符串格式为yyyy-mm-ddThh:mm:ssZ

var date = new Date(); //现在是2012-12-20
document.write(date.toISOString()); //返回2012-12-20T15:45:47.493Z

   


四十一、Date.toJSON      //JSON序列化一个对象     语法:date.toJSON(key)  //date的一个字符串表示形式,值为调用它的toISOString()方法的结果

var date = new Date(); //现在是2012-12-20
document.write(date.toJSON()); //返回2012-12-20T15:45:47.493Z

   


四十二、Date.toLocaleDateString()  //以本地格式的字符串返回一个Date的日期部分语法:date.toLolcaleDateString  //返回一个本地人可读的日期格式,日期部分

var date = new Date(); //现在是2012-12-20
document.write(date.toLocaleDateString()); //返回2012年12月20日

   


 四十三、Date.toLocaleString()    //将一个Date转化难为一个本地格式的字符串 语法:date.toLocaleString()

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleString()); //返回2012年12月22日 19:56:40

   


四十四、Date.toLocaleTimeString()    //将一个Date转化为本地的格式的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleTimeString()); //返回19:57:23

   


四十五、Date.toString()          //将一个Date转换为一个字符串

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十六、Date.toTimeString()       //以字符串的形式返回一个Date对象的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十七、Date.toUTCString()      //将一个Date对象转换为字符串(全球时间)

var date = new Date(); //现在是2012-12-22
document.write(date.toUTCString()); //返回Sat, 22 Dec 2012 12:00:42 GMT

   


四十八、Date.UTC()        //将一个Date对象转换毫秒的形式  静态方法语法:Date.UTC(year,month,day,hours,minutes,seconds,ms)

document.write(Date.UTC(2011, 11, 11, 11, 11, 11)); //返回1323601871000


四十九、Date.valueOf() 

//如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示

var date = "";
document.write(date.valueOf()); //不是Date对象,不输出
var date1 = new Date();
document.write(date1.valueOf()); //输出1356180400916

   



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.