Javascript将日期转换为罗马日期

时间:2018-04-10 13:42:12

标签: javascript

我有以下代码将日期输入转换为罗马日期:

function romanize (num) {
  if (!+num)
    return false;
  var digits = String(+num).split(""),
    key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
           "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
           "","I","II","III","IV","V","VI","VII","VIII","IX"],
    roman = "",
    i = 3;
  while (i--)
    roman = (key[+digits.pop() + (i * 10)] || "") + roman;
  return Array(+digits.join("") + 1).join("M") + roman;
}

$(document).on("change",'#date', function() {
    var date = new Date($('#date').val());
    day = date.getDate();
    month = date.getMonth() + 1;
    year = date.getFullYear();
    var strRomanDate = romanize(month) + " " + romanize(day) + " " + romanize(year); 
    $('#romandate .date').html(strRomanDate);
});

现在,这在某些日期工作正常,例如:

10/04/2018 --> X IV MMXVIII

但是当我想在12之后选择一天,那么13,14,......它返回false false false

任何人都知道我应该在代码中更改哪些内容以使其适用于每个日期?

0 个答案:

没有答案