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 does js judge the number of days?

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to judge the date and days of js". In the course of the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic: given 5 dates of the same format, how to judge whether they are 5 consecutive days?

The data are as follows:

Let days = ['2016-02-28,' 2016-02-29, / / leap month '2016-03-01, / / across the month' 2016-03-02, '2016-03-03,]

Seeing this topic, I immediately thought of another topic: an array, in which there are five numbers, how to judge whether these five numbers are continuous or not:

Let arr = [1, 2, 3, 4, 5]

I feel that the solution of these two problems is the same, and the latter figure is relatively simple, so we don't have to deal with the problem of time. Let's solve the latter problem first.

So what is the train of thought? We can assume that the five numbers in the arr array are contiguous, and then based on that, we do something, what do we do?

If five numbers are continuous, then the first number plus 4, the second number plus 3, the third number plus 2, the fourth number plus 1, the fifth number plus 0, and the final result:

Let newarr = [5, 5, 5, 5, 5, 5]

Then determine whether each number is equal. If it is equal, then arr is continuous, and vice versa.

Here's the idea, and the code is as follows:

Let arr = [1, 2, 3, 4, 5]

Let newarr = arr.sort () .map ((eMagneI) = > {return eigen4muri}) let stand = newarr [0]

Let isTrue = newarr.every (e = > {return e===stand})

Console.log (isTrue)

Next, let's see how to judge whether the dates are continuous or not:

Let days = ['2016-02-28,' 2016-02-29, / / leap month '2016-03-01, / / across the month' 2016-03-02, '2016-03-03,]

Looking at the above array of dates, we assume that the dates are contiguous, and then process each date, but what about it?

The above numerical title is more intuitive, assuming that the numbers are continuous and set to the same number, and the date needs to be dealt with API using the date of js. We can use the setDate method of the Date object to set it to the same date.

To put it simply, set 2016-02-28, 2016-02-29 and other dates to 2016-03-03, we can do this, the code is as follows:

Let dt = new Date ("2016-02-28"); dt.setDate (dt.getDate () + 4-1) console.log (dt)

Print the results:

Now that you have a way to deal with dates, process all the dates in a given array, and the final code is as follows:

Let days = ['2016-02-28,' 2016-02-29, / / leap month '2016-03-01, / / across the month' 2016-03-02, '2016-03-03,]

Let newdays = days.map ((eMagin I) = > {let dt = new Date (e); dt.setDate (dt.getDate () + 4-I); / / convert to timestamp return + dt}) let stand = newdays [0] let isTrue = newdays.every (e = > {console.log (eMagol stand) return stand = e}) console.log (isTrue)

Go a little deeper. What if the time given is like this?

Let days = ['2016-02-28 12-0-02-28 12-0-0-0,' 2016-02-29 12-0-0, / leap-month '2016-03-01 12-0, 0-00, / / across the month,' 2016-03-02 12 00, 0-3, '2016-03-03, 12-00, 0-4,' 2016-03-04, 12, 00, 04,]

We only need to deal with the hours, minutes, seconds and milliseconds of time. The code is as follows:

Let days = ['2016-02-28 12-0-02-28 12-0-0-0,' 2016-02-29 12-0-0, / leap-month '2016-03-01 12-0, 0-00, / / across the month,' 2016-03-02 12 00, 0-3, '2016-03-03, 12-00, 0-4,' 2016-03-04, 12, 00, 04,]

Let newdays = days.map ((e)) = > {let dt = new Date (e); dt.setDate (dt.getDate () + 4-I); dt.setHours (0); dt.setMinutes (0); dt.setSeconds (0); dt.setMilliseconds (0) Return + dt}) let stand = newdays [0] let isTrue = newdays.every (e = > {return stand = e}) console.log (isTrue) "how js determines the number of days and dates" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report