Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getNearDate(date){
  2.     let near_date = new Date(date);
  3.     let dayOfMonth = date.getDate();
  4.     if(dayOfMonth <= 5)
  5.         near_date.setDate(5);
  6.     else if(dayOfMonth <=20)
  7.         near_date.setDate(20);
  8.     else{
  9.         near_date.setDate(5);
  10.         near_date.setMonth(date.getMonth()+1);
  11.     }
  12.  
  13.     let dayOfWeek = near_date.getDay();
  14.     if(dayOfWeek == 0)
  15.         //Если воскресенье
  16.         near_date.setDate(near_date.getDate()-2);
  17.     else if(dayOfWeek == 6)
  18.         //Если суббота
  19.         near_date.setDate(near_date.getDate()-1);
  20.     return near_date;
  21. }
  22.  
  23. function createDate(day, month, year){
  24.     //Принимает день и месяц в НОРМАЛЬНОЙ нотации
  25.     let date = new Date;
  26.     date.setFullYear(year);
  27.     date.setMonth(month-1);
  28.     date.setDate(day);
  29.     return date;
  30. }
  31.  
  32. function getDiffByDays(d1,d2){
  33.     return Math.abs(Math.round((d1 - d2)/(3600*1000*24)));
  34. }
  35.  
  36. function printDate(date){
  37.     console.log(date.getDate(), date.getMonth()+1, date.getFullYear());
  38. }
  39.  
  40. function getBeginDateByDatePeriod(datePeriod){
  41.     //2018-06-01/2018-06-30
  42.     let tempStr = datePeriod.slice(0,10);
  43.     let year = tempStr.slice(0,4);
  44.     let month = tempStr.slice(5,7);
  45.     let day = tempStr.slice(8,10);
  46.     return createDate(day,month,year);
  47. }
  48.  
  49.  
  50. printDate(getBeginDateByDatePeriod("2018-06-01/2018-06-30"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement