Show the date using Roman numerals

I'm looking to showcase the current date on my website using jquery or javascript, which seems like a simple task, right?

However, I want the date to be presented in roman numerals (d/m/y format). For example, instead of 13/10/2013, I would like it displayed as XIII.X.MMXIII

I've been attempting this for a few days now, but nothing I try seems to work. My knowledge of jquery and javascript is fairly limited, and I only know how to display the 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 assist me with displaying the date in roman numerals, I would greatly appreciate it.

Thank you.

Answer №1

Explore one of the roman numeral converters mentioned in this query Convert a number into a Roman Numeral in JavaScript. For instance, consider using the converter from :

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

Following that, you can execute:

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

Answer №2

    var currentDay = new Date()
    var monthNum = currentDay.getMonth() + 1
    var dayNum = currentDay.getDate()
    var yearNum = currentDay.getFullYear()
    document.write(convertToRoman(monthNum) + " . " + convertToRoman(dayNum) + " . " + convertToRoman(yearNum))

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

    var resultRoman = '';
    for (let index = 0; index < numeralsList.length; index++) {
        while (number >= numeralsList[index]) {
            resultRoman += romanNumerals[index];
            number -= numeralsList[index];
        }
    }
    return resultRoman;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the outcome of each element in a JavaScript forEach loop by incorporating values from a

I'm struggling to adjust the output of my forEach loop using values from another array. Despite my efforts, I haven't been able to make it work. const BbDescriptionDictionary = ['AAA' , 'BBB', 'CCC',] const boardBa ...

Enhance Your AngularJS Application with Data Transfer Object Models

Is there a way to implement a Data Transfer Object (DTO)? In my backend code, I have clearly defined domains such as the Client class: class Client { protected $firstName; protected $lastName; } This class contains specific properties that I wan ...

The cascading menu continuously scrolls upwards

After incorporating this feature into my website following a previous inquiry, I am encountering an issue with the dropdown box unexpectedly scrolling upwards in Firefox and IE browsers. It's baffling me! If you click on News Feed, the dropdown is su ...

Utilizing additional JavaScript libraries with custom Power BI visuals

Seeking clarification on how to use the d3 library within my visual.ts file. I have installed it using npm and added it to the externalJS section of pbiviz.json, but I am unsure of any additional configurations needed to successfully include and utilize it ...

Record the user actions from logging in through Facebook or OAuth and store them in the database

I have been attempting to log in with Facebook using Firebase. How can I save user information such as email and username to the database? Additionally, I am unsure of what steps to take next when users engage in activities like ordering products on my w ...

MetaMask RPC error encountered: execution failed with code -32000 and message stating "execution reverted"

While working on a DApp project that involves using React and Solidity with an ERC20 contract, I have successfully deployed my smart contract on Rinkeby. I am able to interact with the contract using name() and symbol(). However, I encountered an issue whe ...

What could be causing this Angular dependency to go unnoticed?

I am currently developing an angular application and organizing my logic into separate files. I have created stub modules in app.js, services.js, and controllers.js with the intention of implementing them in individual files. However, I am facing difficul ...

Implementing jquery to show information in a dropdown menu

I would like to populate a select box with data from an AJAX response. Below is the code from my view file: <select class="form-control" name="vendor" id="vendor_list" required style="width: 159px;"> <option value="">Vendor 1</option> & ...

Guide on implementing real-time search functionality with the Fetch Ajax method in Django using Python

Help! I'm new to JavaScript and keep getting a Post.match is not a function error :( I need to turn an object into an array, but using Objects.values Method still gives me this error. Can someone please assist me? Here's my Views.py File: from d ...

Endless polling using Angular 4 and Http observables

I am currently developing an infinite polling feature in my Http service for a dashboard that receives survey data from a server. The code I have written below is almost functional (I can see the Json data coming in the console, but it is not reflecting ...

What is preventing these AngularJS applications from functioning simultaneously?

I have a fully functioning AngularJS app that I developed as a standalone "CreateUser" widget. Now, I am working on creating a second widget called "ViewUsers," which will display a table of current users (with the intention of connecting them or keeping t ...

Tips and tricks for implementing vuetify tooltips within a list component

Looking for help with my code: <div id='app'> <v-app> <v-list-tile-content> <v-list-tile v-for="(item, index) in items" :key="item.id" > <v-list-tile-action> {{index+1}}. </v-list-tile-action> < ...

Creating Twillio access codes with NodeJS

As I work on a project integrating Twillios Programmable Video API, I find myself navigating through the Node JS documentation for the first time. It's been quite clear so far, but I do have a couple of lingering questions. Below is the code snippet ...

How can I properly execute json requests and save data when iterating over an array?

I'm currently trying to loop through an array of addresses in order to request their respective geolocations using the Google Maps Geocode API. However, I'm facing an issue with pushing the results (latitude and longitude coordinates) into an arr ...

Group-level selection in SlickGrid's multi-level grouping feature

https://i.sstatic.net/NE6MQ.png Incorporating a slick grid with a personalized selection model and a unique checkbox selection plugin has been a successful endeavor. Additional group level checkboxes have been included to facilitate toggling selections at ...

Create a download button for a PDF table in landscape format

Hey everyone, I've been working on creating a dynamic timetable for a group without the need for pen and paper using JavaScript, HTML, and CSS. One challenge I'm facing is figuring out how to add a button that allows users to download the table i ...

Retrieving data response post upload with JQuery and an HTML5 Uploader

After uploading an image and receiving a successful post response with the id of the inserted image, how can I insert this response as a data-id attribute within a a tag? Where in the function does this process occur? Here is the function: function img_ ...

Utilizing Google Maps API Version 3 to create interactive infoBubbles using an

Are there any recommended Google Maps v3 infoBubble examples that anyone has come across or knows of? I am interested in utilizing an array of data for this, but I'm unsure if there are any effective applications available or if it can be easily impl ...

One-page website featuring a scrolling layout and a stable, constantly updating footer

My goal is to create a single-page, scrolling website that features an array of images with corresponding captions fixed at the bottom of the page. I plan on using unique image IDs to trigger a hide/show event as each image approaches a certain distance fr ...

Preventing child rendering in React until the state is updated: best practices

I'm currently developing a navigation feature that reveals the second tier of options once a menu item is clicked. Below is an example of my code: class App extends Component { constructor(props) { super(props); this.state = { curren ...