日期时间星期实现多语言转化

同时发现 d3.timeFormat("%Y-%m-%d")(new Date(toDate.getTime() - i * 24 * 3600 * 1000));在不需要语言切换的时候可以直接对中国标准时间进行转化,默认英文。

const WEEKDAYSABBR=[
  {
    "en": "Sun",
    "zh": "星期日",
  },
  {
    "en": "Mon",
    "zh": "星期一",
  },
  {
    "en": "Tue",
    "zh": "星期二",
  },
  {
    "en": "Wed",
    "zh": "星期三",

  },
  {
    "en": "Thu",
    "zh": "星期四",
  },
  {
    "en": "Fri",
    "zh": "星期五",
  },
  {
    "en": "Sat",
    "zh": "星期六",
  },
];
const MONTHS = [
  {
    "en": "Jan",
    "zh": "一月",
  },
  {
    "en": "Feb",
    "zh": "二月",
  },
  {
    "en": "Mar",
    "zh": "三月",
  },
  {
    "en": "Apr",
    "zh": "四月",
  },
  {
    "en": "May",
    "zh": "五月",
  },
  {
    "en": "Jun",
    "zh": "六月",
  },
  {
    "en": "Jul",
    "zh": "七月",
  },
  {
    "en": "Aug",
    "zh": "八月",
  },
  {
    "en": "Sep",
    "zh": "九月",
  },
  {
    "en": "Oct",
    "zh": "十月",
  },
  {
    "en": "Nov",
    "zh": "十一月",
  },
  {
    "en": "Dec",
     "zh": "十二月",
  }
];

var lang;
!function () {
    lang = navigator.language || navigator.userLanguage;//常规浏览器语言和IE浏览器
    lang = lang.substr(0, 2);//截取lang前2位字符
}();


//星期
logbook.getWeekdayAbbr = function (index, lang) {
  return WEEKDAYSABBR[index][lang];
};

// 月份
logbook.getMonthName = function (index, lang) {
  return MONTHS[index][lang];
}

// 格式化日期 dayOfWeek
logbook.formatLogDate = function (date, lang) {
  let dateWeeks=logbook.getWeekdayAbbr(date.getDay(), lang);
  return dateWeeks;
  //return `${logbook.getWeekday(date.getDay(), lang)}`;
}

// 格式化日期 month day
logbook.formatLogMonth = function (date, lang) {
  return `${logbook.getMonthName(date.getMonth(), lang)}, ${date.getDate()}`;
}
function(d) { return logbook.formatLogMonth(new Date(d.date), lang) }

你可能感兴趣的:(React)