用罗马数字显示日期

时间:2013-10-13 05:11:49

标签: javascript

我需要使用jquery或javascript在我的网页上显示当前日期,听起来很简单吗?

但是我希望日期以罗马数字(d / m / y格式)显示。例如:13/10/2013显示为XIII.X.MMXIII

我已经尝试了几天,但我尝试的一切都行不通。我对jquery和javascript的知识相当有限,我只知道如何做正常的约会。像这样:

<script type="text/javascript">
    <!--
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(month + " . " + day + " . " + year)
    //-->
  </script>

如果有人可以帮我用罗马数字显示日期,我们将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

使用此问题Convert a number into a Roman Numeral in javaScript中讨论的罗马数字转换器之一。 例如,来自http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter的那个:

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;
}

然后你可以这样做:

var currentTime  = new Date();
var strRomanDate = romanize(currentTime.getMonth() + 1) + " . " + 
                   romanize(currentTime.getDate())      + " . " +
                   romanize(currentTime.getFullYear()) + 1; 

答案 1 :(得分:0)

    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(toRoman(month) + " . " + toRoman(day) + " . " + toRoman(year))

function toRoman(num) {
    var listOfNum = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    var listOfRoman = ['M', 'CM', 'D', 'CD', "C", 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']

    var numToRoman = '';
    for (let i = 0; i < listOfNum.length; i++) {
        while (num >= listOfNum[i]) {
            numToRoman += listOfRoman[i];
            num -= listOfNum[i];
        }
    }
    return numToRoman;
}