Date objects commonly used packaging methods and how to solve problems

Xiaobian to share a common package method commonly used by DATE objects and how to solve it, I hope everyone will read this article, let us discuss together!

Using the Date object in JS has been a long time, but it has not been recorded for the common package function and the pit encountered. When you are free today, you will record it, it is convenient for yourself. And remind yourself to encounter those pits. If there is any places wrong, I hope everyone can point out, then I will be grateful.

When the date (no time division) is converted to a timestamp, the date is converted to time when connecting with ‘-‘ (2019-01-01) and ‘/’ (2019/01/01). The results of the stamp are different

In order not to let everyone see too much, they will be conclusions.
Conclusion:
1) If the date is used in the ‘-‘ connection, the number of days and days are less than 9 and the previous plus one is added, and the time is converted to the timestamp to convert the time to the time. At 8 o’clock in the morning
2) If the date is used in the ‘-‘ connection, the number of days and days have a less than 9 and less than 9, then it will be turned to timestamp. Time Default Convert to 8:00 am
3) If the date is used in the ‘-‘ connection, the number of days and days are less than 9 and only one of the previous adds 0, then it is turned to timestamp. It will convert the time by default to 12:00 am to the day, 00:00
4) When the date is used in ‘-‘ connection, the number of days and days are greater than 9, then it is turned to timestamp. Converting time to 8 pm when the day is 8 o’clock
5) If the date is used in ‘/’ connection, it will only be converted to the time stamp to the day of the day 00:00
summary: When the date is converted to a timestamp, if the time is not set, it is best to use ‘/’ to connect to avoid the same date to obtain the timestamp acquired when the write method is different
is used to prove the conclusion:

“LET Time1 = New Date (‘2019-03-03’). getTime (); let time2 = new date (‘2019/3/3’). getTime (); console.log (‘Get Time’) Console.log (Time1) Console.log (Time2) Console.log (Time1-Time2) / 1000/60) // 8 // According to the local format, convert the date object time to a string at 12:00:00 in the morning 00:00:00 Console.log (New Date (‘2019-03-03’). Tolocalestring ()) // 2019/3/3 8:00:00 Console.log (New Date (‘2019-03 -12 ‘). Tolocalestring ()) // 2019/3/12 8:00:00 Console.log (New Date (‘ 2019-11-03 ‘). Tolocalestring ()) // 2019/11/3 am 8:00:00 Console.log (New Date (‘2019-3-03’). Tolocalestring ()) // 2019/3/3 12:00:00 Console.log (New Date (‘2019-03- 3 ‘). Tolocalestring ()) // 2019/3/3 12:00:00 Console.log (New Date (‘ 2019-11-13 ‘). Tolocalestring ()) // 2019/11/13 AM 8 am 00: 00 Console.log (New Date (‘2019 /03/03 ‘). Tolocalestring ()) // 2019/3/3 12:00:00 Console.log (New Date (‘ 2019/3/3 ‘). Tolocalestring ()) // 2019/3/3 At 12:00:00 am Console.log (New Date (‘2019/03/3’). Tolocalestring ()) // 2019/3/3 12:00:00 Console.log (New Date (‘2019/3 / 03 ‘). TOLOCALESTRING ()) // 2019/3/3 12:00:00 Console.log (New Date (‘ 2019/03/12 ‘). Tolocalestring ()) // 2019/3/12 AM 12:00:00 Console.log (New Date (‘2019/11/03’). Tolocalestring ()) // 2019/11/3 12:00:00 am “
   
Convert the date format to timestamp and convert the timestamp to date format

1. Convert date format to timestamp three methods “JavaScript let DateStr = New Date” 2019-3-20 18: 59: 39: 123 ‘); let timestamp1 = datestr.gettime (); // 1553079579123 let timestamp2 = datestaueof (); // 1553079579123 let timestamp3 = date.parse (datestr); / / 1553079579000 “DATE.getTime (), Date.Valueof () will be accurate to milliseconds, and Date.Parse (date) can only be accurate to second, for milliseconds 2. Translate timestamp to date format “JavaScript function dateformat (timestamp) {TimeStamp = (TimeStamp + ”) .length> 10? TimeStamp: TimeStamp * 1000; // Judgment timestamp as a few digits, 10 hours plus milliseconds, 13 is the let Date = new date (timestamp); letry = Date.GetFullyEar (); let month = Date.getMonth () + 1> 9? Date.getMonth () + 1: ‘0’ + (Date.getMonth () + 1); // From 0 start, 0 ~ 11, so +1 let day = Date.getdate (): ‘0’ + Date.getdate (): ‘0’ + Date.getdate (): ‘0’ + Date.getdate (): ‘0’ + Date.getdate (); Let Hour = Date.GetDate ()> 9? Date.GetHours : ‘0’ + Date.GetHours (); Let Minutes = Date.getMinutes ()> 9? Date.getminutes (): ‘0’ + Date.getMinutes (); let seconds = DATE.GETSECONDS ()> 9? Date.getSeconds (): ‘0’ + Date.getSeconds (); RETURN YEAR + ‘-‘ + MONTH + ‘-‘ + DAY + ” + HOUR + ‘:’ + Minutes + ‘:’ + Seconds;} “
   
Compare two dates How many days of symnary

/ ** * @Method calculates between two dates, including the first day * @Param Begintime start time ‘2019-3- 19 ‘||’ 2019/3/19 ‘* @Param EndTime End Time Date’ 2019-3-20 ‘||’ 2019/3/19 ‘* / GetInterValday (‘ 2019-03-03 ‘,’ 2019- 03-8 ‘); // If there is no result, the result of the format conversion is 5 days, the conversion is 6 days Function GetInterValday (begintime, endtime) {// First use it to a unified format, because’ – The result of the timestamp conversion under the format is inconsistent, the reason is based on the beginning of this article BeGintime = Begintime.Replace (/ \ – / g, ‘/’); endtime = endtime.replace (/ \ – / g, ‘/’) Let Time1 = New Date (Begintime) .gettime (); Let Time2 = New Date (endtime) .gettime (); // console.log (begintime) // console.log (endtime) let second = Time2 – Time1; Let day = parseint (Second / (1000 * 60 * 60 * 24)) + 1; // On the day, it is also necessary to go in Return Day;}
 How many days have been judged   
// Leap year is 366 days (more than one day in February), the year is 365 days. // Leap year has two: 1) Ordinary leap year: It can be 4 unparalleled but not by 100, the year is ordinary leap year. // 2) Century leap year: It can be taled by 400 for the century leap year. Function getYeralLDay (Year) {return (Year% 4 === 0 && year% 100! == 0) || Year% 400 === 0? 366: 365;}

 Get a month of one month in a year   
// Date format is’ xxxx-xx-xx ” xxxx / xx / xx ” xxxx / xx ” XXXX-XX ‘Function GetMonthallDay (Date) {date = new date (date); letry = Date.getFullyear (); let month = Date.getMonth () + 1; // Return from the DATE object (0 ~ 11). Let NextMonth = Year + ‘-‘ + 1); let newdate = new date (nextmonth); newdate.setdate (0); // Set from 1 to 31 when setting up, when set to 0, ie The last day of last month return newdate.getdate ();}

After reading this article, I believe that you have a certain understanding of the packaging methods and problems that are commonly used by Date objects. I want to know more related knowledge. Welcome to pay attention. Tumi Yun Industry Information Channel, thank you for reading!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment