javascript JS - 日期功能(WIP 20190115)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JS - 日期功能(WIP 20190115)相关的知识,希望对你有一定的参考价值。

(function() {
  let dateText = document.getElementById("today-tomorrow");
  let todayDate =  new Date(); // Store date object for today
  let tomorrowDate = new Date();
  tomorrowDate.setDate(todayDate.getDate()+1); //Store date object for tomorrow

  //Store numerical hours, day & month values for today
  let todayMonth = todayDate.getMonth() + 1;
  let todayDay = todayDate.getDate();

  //Store numberical day & month values for tomorrow
  let tomorrowMonth = tomorrowDate.getMonth() + 1;
  let tomorrowDay = tomorrowDate.getDate();

  //If it's before 1pm, it ships within 24 hours, otherwise it ships tomorrow.
  if (todayDate.getHours() < 13) {
      //insertText( dateText, "today, " + getMonthName(todayMonth-1) + " " + getDateText(todayDay) );
      insertText( dateText, "{{lang 'single_product_page.hours'}}");
  //} else if ( (!(todayDate.getHours() < 13)) && ( todayDay.getDay > 4 ) ) {
  //    //insertText( dateText, "tomorrow, " + getMonthName(tomorrowMonth) + " " + getDateText(tomorrowDay) );
  //    insertText( dateText, "on Monday.");
  } else {
      insertText( dateText, "{{lang 'single_product_page.tomorrow'}}");
  }

  //Inserts text into an element via the innerHTML property.
  function insertText (element, text) {
      element.innerHTML = text;
  }

  //Takes in a numerical day of the week and returns the text version.
  function dayOfWeekText (dayNumber) {
      switch (dayNumber) {
          case 0:
              return "Sunday";
          case 1:
              return "Monday";
          case 2:
              return "Tuesday";
          case 3:
              return "Wednesday";
          case 4:
              return "Thursday";
          case 5:
              return "Friday";
          case 6:
              return "Saturday";
          default:
              return "Wrong Date Number";
      }
  }

  //Takes in a numerical month and returns the name of the month.
  function getMonthName (monthNumber) {
      let months = ["{{lang 'general.months.jan'}}", 
                    "{{lang 'general.months.feb'}}",
                    "{{lang 'general.months.mar'}}",
                    "{{lang 'general.months.apr'}}",
                    "{{lang 'general.months.may'}}",
                    "{{lang 'general.months.jun'}}",
                    "{{lang 'general.months.jul'}}",
                    "{{lang 'general.months.aug'}}",
                    "{{lang 'general.months.sep'}}",
                    "{{lang 'general.months.oct'}}",
                    "{{lang 'general.months.nov'}}",
                    "{{lang 'general.months.deck'}}"];
      return months[monthNumber];
  }

  //Takes in a numerical day of the month and returns the text version with suffix.
  //example ==> passing a 1 would return '1st', passing a 15 would return '15th'.
  function getDateText (dateNumber) {
      if (dateNumber) {
          return dateNumber + dateSuffix(lastDigit(dateNumber));
      }
  }

  //Returns last digit of a given number, in the case of a single digit number,
  //returns that single digit.
  function lastDigit (number) {
      if (number < 10) {
          return number;
      } else {
          return number % 10;
      }
  }

  //Takes in a single digit and returns a string containing the appropriate suffix such as 'st', 'nd', 'rd', etc.
  //example: 1 returns 'st', 2 returns 'nd'
  function dateSuffix (lastDigit) {
      switch (lastDigit) {
          case 1:
              return "st";
          case 2:
              return "nd";
          case 3:
              return "rd";
          default:
              return "th";
      }
  }
})();

以上是关于javascript JS - 日期功能(WIP 20190115)的主要内容,如果未能解决你的问题,请参考以下文章

javascript WIP

[WIP] nvm 的安装与使用

求一个js日期时间控件,我有一个例子,但功能不能实现

Javascript扩展String.prototype实现格式金额格式时间字符串连接计算长度是否包含日期计算等功能

Git 是离开 WIP 分支并开始在另一个分支上工作的更好方法

JS返回当前日期