Javascript date in Background

Javascript date in Background
0

#1

Hi Everyone ,

I am trying to build a chrome extension that loads a random image from a folder and on every load of the page , it must show the date in DD-MM-YYYY format. I have got the javascript code for the image to load. However I tried using Moment.js for the Javascript date and it does not seem to work. Can anyone of you guys here help me with it ? Any help is appreciated

Anirudh


#2

Can you post the code you have tried?


#4

Sure here is the JS Code

var totalCount = 30;
function ChangeIt()
{
document.body.style.backgroundRepeat = “no-repeat”;
document.body.style.backgroundSize = “cover”;
var num = Math.ceil( Math.random() * totalCount );
document.body.background = ‘images/’+num+’.jpg’;
}
ChangeIt()


#5

I don’t see where you are trying to display a date anywhere in the above code.


#8

For some reason , the date function is not copying properly in the editor. I am using moment.js

var today = moment().format('LL');
var date = document.getElementById('now').innerHTML = today;

#9

When I test the above code using the moment.js library, I see July 23, 2018 displayed in the element I created with id=“now”.

If I change the code to the following, then I get what you original asked for which is 23-07-2018

var today = moment().format('DD-MM-YYYY');
var date = document.getElementById('now').innerHTML = today;

When you load the page that this code is on, have you looked at the browser’s console to check for error messages? Are you sure you have properly loaded the moment.js library into the page?


#10

Yeah thanks for the code. I solved the problem. The problem was that I tried to load all the random images as well the date function on 1 js file. I seperated them into js files and it seems to work fine. Thanks a lot anyways