简体   繁体   中英

Display date in roman numerals

I need to display the current date on my webpage using jquery or javascript, sounds easy right?

However I would like the date to be displayed in roman numerals (d/m/y format). eg: 13/10/2013 to be displayed as XIII.X.MMXIII

I have been trying for a few days now but everything I try won't work. I have a fairly limited knowledge of jquery and javascript, I only know how to do normal date. Like this:

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

If anyone can help me to display the date in roman numerals it would be greatly appreciated.

Thank you.

Use one of the roman numeral converter discussed in this question Convert a number into a Roman Numeral in javaScript . By example, the one from 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;
}

Then you can do:

var currentTime  = new Date();
var strRomanDate = romanize(currentTime.getMonth() + 1) + " . " + 
                   romanize(currentTime.getDate())      + " . " +
                   romanize(currentTime.getFullYear()) + 1; 
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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM