date.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const moment = require('moment-timezone');
  3. const { isMoment } = moment;
  4. const isDate = value =>
  5. typeof value === 'object' && value instanceof Date && !isNaN(value.getTime());
  6. function getMoment(date, lang, timezone) {
  7. if (date == null) date = moment();
  8. if (!isMoment(date)) date = moment(isDate(date) ? date : new Date(date));
  9. lang = toMomentLocale(lang);
  10. if (lang) date = date.locale(lang);
  11. if (timezone) date = date.tz(timezone);
  12. return date;
  13. }
  14. function toISOString(date) {
  15. if (date == null) {
  16. return new Date().toISOString();
  17. }
  18. if (date instanceof Date || isMoment(date)) {
  19. return date.toISOString();
  20. }
  21. return new Date(date).toISOString();
  22. }
  23. function dateHelper(date, format) {
  24. const { config } = this;
  25. const moment = getMoment(date, getLanguage(this), config.timezone);
  26. return moment.format(format || config.date_format);
  27. }
  28. function timeHelper(date, format) {
  29. const { config } = this;
  30. const moment = getMoment(date, getLanguage(this), config.timezone);
  31. return moment.format(format || config.time_format);
  32. }
  33. function fullDateHelper(date, format) {
  34. if (format) {
  35. const moment = getMoment(date, getLanguage(this), this.config.timezone);
  36. return moment.format(format);
  37. }
  38. return `${this.date(date)} ${this.time(date)}`;
  39. }
  40. function relativeDateHelper(date) {
  41. const { config } = this;
  42. const moment = getMoment(date, getLanguage(this), config.timezone);
  43. return moment.fromNow();
  44. }
  45. function timeTagHelper(date, format) {
  46. const { config } = this;
  47. return `<time datetime="${toISOString(date)}">${this.date(date, format, getLanguage(this), config.timezone)}</time>`;
  48. }
  49. function getLanguage(ctx) {
  50. return ctx.page.lang || ctx.page.language || ctx.config.language;
  51. }
  52. /**
  53. * Convert Hexo language code to Moment locale code.
  54. * examples:
  55. * default => en
  56. * zh-CN => zh-cn
  57. *
  58. * Moment defined locales: https://github.com/moment/moment/tree/master/locale
  59. */
  60. function toMomentLocale(lang) {
  61. if (lang === undefined) {
  62. return undefined;
  63. }
  64. // moment.locale('') equals moment.locale('en')
  65. // moment.locale(null) equals moment.locale('en')
  66. if (!lang || lang === 'en' || lang === 'default') {
  67. return 'en';
  68. }
  69. return lang.toLowerCase().replace('_', '-');
  70. }
  71. exports.date = dateHelper;
  72. exports.date_xml = toISOString;
  73. exports.time = timeHelper;
  74. exports.full_date = fullDateHelper;
  75. exports.relative_date = relativeDateHelper;
  76. exports.time_tag = timeTagHelper;
  77. exports.moment = moment;
  78. exports.toMomentLocale = toMomentLocale;