hinatazaka46-dateutil

Hinatazak46 dateeutil

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/537734/1629358/hinatazaka46-dateutil.js

  1. // ==UserScript==
  2. // @name hinatazaka46-dateutil
  3. // @namespace https://greasyfork.org/ja/users/1328592-naoqv
  4. // @description Hinatazak46 dateeutil
  5. // @description:ja 日向坂46 日付ユーティリティライブラリ
  6. // @version 0.05
  7. // @icon https://cdn.hinatazaka46.com/files/14/hinata/img/favicons/favicon-32x32.png
  8. // @compatible chrome
  9. // @compatible firefox
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. "use strict";
  15.  
  16. const last2digitsFromYear = (year) => (((str) => str.substring(str.length - 2))(year.toString()));
  17.  
  18. const weekDay = {
  19. 0: 'Sun.',
  20. 1: 'Mon.',
  21. 2: 'Tue.',
  22. 3: 'Wed.',
  23. 4: 'Thu.',
  24. 5: 'Fri.',
  25. 6: 'Sat.',
  26. };
  27.  
  28. /*
  29. * 年表示を短縮表記して曜日を付加 yyyy.MM.dd hh:mm → 'yy.MM.dd(a) hh:mm
  30. * @param {string} yearSelector - 年表示要素セレクタ
  31. */
  32. const shortenYearWithDay = (yearSelector) => {
  33. Array.prototype.forEach.call(document.querySelectorAll(yearSelector), (x) => {
  34. const text = x.innerText;
  35. const match = text.match(/(\d{4})\.(\d{1,2})\.(\d{1,2})(\s(\d{2}):(\d{2}))?/);
  36.  
  37. if (match && match[3]) {
  38.  
  39. const date = new Date(parseInt(match[1]), parseInt(match[2] - 1), parseInt(match[3]), match[4] ? parseInt(match[4]) : 0, match[4] ? parseInt(match[4]) : 0, 0, 0);
  40.  
  41. if (match[5]) {
  42. x.innerText
  43. = text.replace(/\d{4}\.\d{1,2}\.\d{1,2}\s\d{2}:\d{2}/, `'${last2digitsFromYear(date.getFullYear())}.${date.getMonth() + 1}.${date.getDate()} ${weekDay[date.getDay()]} ${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}`);
  44. } else {
  45. x.innerText
  46. = text.replace(/\d{4}\.\d{1,2}\.\d{1,2}/, `'${last2digitsFromYear(date.getFullYear())}.${date.getMonth() + 1}.${date.getDate()} ${weekDay[date.getDay()]}`);
  47. }
  48. } else {
  49. x.innerText = text.replace(/^(?!')\d{2}(\d{2}\.)/, "'$1");
  50. }
  51. });
  52. };
  53.  
  54. /*
  55. * Excel日付型のシリアル値からDateオブジェクトを生成
  56. * @param {string} seria - シリアル値
  57. */
  58. const excelSerialToDate = (serial) => {
  59. // Excelは1900年1月1日を1として扱うが、JSのDateは1970年が起点
  60. // 1900-01-01 から 1970-01-01 までの日数 = 25569日(ただし、Excelの1900年はうるう年として誤認しているので補正必要)
  61. const msPerDay = 24 * 60 * 60 * 1000;
  62. const excelEpochOffset = 25569;
  63.  
  64. // Excelのシリアル値が整数の場合も小数(時間付き)でも対応
  65. const date = new Date((parseInt(serial) - excelEpochOffset) * msPerDay);
  66. return date;
  67. };
  68.  
  69. /*
  70. * 黄道12星座 定数クラス
  71. */
  72. class ZODIAC {
  73. static AQUARIUS = "みずがめ座";
  74. static PISCES = "うお座";
  75. static ARIES = "おひつじ座";
  76. static TAURUS = "おうし座";
  77. static GEMINI = "ふたご座";
  78. static CANCER = "かに座";
  79. static LEO = "しし座";
  80. static VIRGO = "おとめ座";
  81. static LIBRA = "てんびん座";
  82. static SCORPIO = "さそり座";
  83. static SAGITTARIUS = "いて座";
  84. static CAPRICORN = "やぎ座";
  85. static list = () => new Array(ZODIAC.AQUARIUS, ZODIAC.PISCES, ZODIAC.ARIES, ZODIAC.TAURUS, ZODIAC.GEMINI, ZODIAC.CANCER,
  86. ZODIAC.LEO, ZODIAC.VIRGO, ZODIAC.LIBRA, ZODIAC.SCORPIO, ZODIAC.SAGITTARIUS, ZODIAC.CAPRICORN);
  87. }
  88.  
  89. /*
  90. * 誕生日日付に該当する星座を返す
  91. * @param {date} date - 誕生日
  92. */
  93. const getZodiacSign = (date) => {
  94. const month = date.getMonth() + 1;
  95. const day = date.getDate();
  96.  
  97. if ((month === 1 && day >= 20) || (month === 2 && day <= 18)) return ZODIAC.AQUARIUS;
  98. if ((month === 2 && day >= 19) || (month === 3 && day <= 20)) return ZODIAC.PISCES;
  99. if ((month === 3 && day >= 21) || (month === 4 && day <= 19)) return ZODIAC.ARIES;
  100. if ((month === 4 && day >= 20) || (month === 5 && day <= 20)) return ZODIAC.TAURUS;
  101. if ((month === 5 && day >= 21) || (month === 6 && day <= 20)) return ZODIAC.GEMINI;
  102. if ((month === 6 && day >= 21) || (month === 7 && day <= 22)) return ZODIAC.CANCER;
  103. if ((month === 7 && day >= 23) || (month === 8 && day <= 22)) return ZODIAC.LEO;
  104. if ((month === 8 && day >= 23) || (month === 9 && day <= 22)) return ZODIAC.VIRGO;
  105. if ((month === 9 && day >= 23) || (month === 10 && day <= 22)) return ZODIAC.LIBRA;
  106. if ((month === 10 && day >= 23) || (month === 11 && day <= 21)) return ZODIAC.SCORPIO;
  107. if ((month === 11 && day >= 22) || (month === 12 && day <= 21)) return ZODIAC.SAGITTARIUS;
  108. return ZODIAC.CAPRICORN; // (12/22〜1/19)
  109. };