Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use javascript built-in object Date

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "javascript built-in object Date how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "javascript built-in object Date how to use" it!

Basic use of Date

The built-in object Date is a constructor. The object is created with new. If there is no parameter, the current time is returned. The parameter can be added to return a specific time.

/ / the built-in object Date is a constructor. Create an object with new. Return the current time if there is no parameter. Add parameters to return a specific time var now_time = new Date (); console.log (now_time); / / you can use-or / var time_1 = new Date between years, months and days ('2018-05-06 12 console.log 36 time_1 15'); var time_2 = new Date ('2018 console.log 06'); console.log (time_1); console.log (time_2)

Formatting time

You can see from the picture above that the returned time format Sun May 06 2018 00:00:00 GMT+0800 (China Standard time) is not in line with our Chinese habits.

So we need to get the specified part of the date and format the date.

The method name description code getFullYear () gets the current year Obj.getFullYear () getMonth () gets the current month, returns 0-11Obj.getMonth () getDate () to get the date of the day Obj.getDate () getDay () gets the day of the week (Sunday 0 to Saturday 6) Obj.getDay () getHours () gets the current hour Obj.getHours () getMinutes () gets the current minute Obj.getMinutes () getSeconds () gets the current second Obj.getSeconds () 1. Format date-year, month, day / / format date-year, month, day var date = new Date (); / / instantiate a date object var year = date.getFullYear (); / / return current date year var month = date.getMonth () + 1; / / return 0-11, corresponding to 1-12 months, so receiving + 1 is the correct number of months year = year

< 10? '0' + year: year ;var dates = date.getDate(); // 返回几号dates = dates < 10? '0' + dates: dates ;var day = date.getDay(); //返回的是0-6,分别对应星期天-星期六var day_arr = ['星期天','星期一','星期二','星期三','星期四','星期五','星期六',]console.log('今天是:'+ year + '年' + month + '月' + dates + '日' + day_arr[day]);

two。 When formatting, function getTime () {var time = new Date (); var h = time.getHours (); h = h < 10?'0' + h: h; var m = time.getMinutes (); m = m < 10?'0' + m: m; var s = time.getSeconds () S = s < 10?'0' + s: s; return h +':'+ m +':'+ s;} console.log (getTime ())

Get the total milliseconds (timestamp) of the Date

We can often see the time stamp, so how did he get here?

In fact, the timestamp represents the total number of milliseconds from January 1, 1970 to the current time.

As for why it was this time in 1970, if you are interested, you can take a look at Baidu, which is very interesting.

There are three ways to get a timestamp in our javascript.

/ / get the total number of milliseconds (timestamp) of Date, which is the total number of milliseconds from January 1, 1970 to the present. As for why this is the time, please take a look at Baidu. It's very interesting / 1. Through the valueOf () or getTime () method var date = new Date (); console.log (date.valueOf ()); / / the total milliseconds of our current time distance of 1970 console.log (date.getTime ()); / / 2. Simple writing (most commonly used) var date = + new Date (); console.log (date); / / added by 3.H5, console.log (Date.now ()) is not applicable below ie9

Case-web page countdown core algorithm (important)

1) Core algorithm: the input time minus the present time is the time of profit, that is, the countdown, but you can't subtract the minutes and seconds, for example, 05 minutes minus 25 minutes, the result will be negative.

2) use time to do it. The user enters the total number of old seconds of the time minus the total number of milliseconds of the current time, and gets the milliseconds of the remaining time.

3) convert the total number of milliseconds of the remaining time into seconds and then convert them into days, hours, minutes and seconds (time into hours and seconds)

Under the conversion formula:

D = parselnt (total seconds / 60-60-24); calculated days

H = parselnt (total seconds / 60ticks 60% 24); calculated hours

M = parselnt (total seconds / 60% 60); calculate score

S = parselnt (total seconds 60); calculate current seconds

/ / Web page countdown core algorithm function countTime (time) {var newTime = + new Date (); / / get the current total number of milliseconds (timestamp) var inputTime = + new Date (time); / / get the timestamp of the specified time var times = (inputTime-newTime) / 1000; / / seconds of remaining time var d = parseInt (times / 60 / 60 / 24); / / days, rounded d = d

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report