0

I've a phoneGap build project, that open a specific page everyday by taking date value from phone.

Its like one year's full todo list is written down on 365 pages and i want to show one page everyday that matches present day's date.

I've 365 HTML pages like 1.html 2.html so on.. when user opens the app, it should check the date and open matching html page accordingly.

For ex: if today's date is Jan 1st, when user open app it must open 1.html. if its Jan two it must open 2.html and so on.

I've tried this:

document.addEventListener('DOMContentLoaded', function() {
open() }, false);

function open(){
window.open ('1.html','_self',false);
}

it wont work and i don't know How to keep today's date value in a variable, compare it and pass result to open specific page accordingly.

Can someone help me how to achieve this? should i use arrays? or if statements? please help.. Much appreciated .

  • Probably a duplicate of [*Calculating Jday(Julian Day) in javascript*](http://stackoverflow.com/questions/11759992/calculating-jdayjulian-day-in-javascript). – RobG Feb 13 '17 at 23:44

2 Answers2

0

Just create a function to get the day number of current date like :

 var dayNumber = function(){
    var now = new Date();
    var start = new Date(now.getFullYear(), 0, 0);
    var diff = now - start;
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);
    console.log('Day of year: ' + day);
  }

So after that you can open the html page you want like that :

document.addEventListener('DOMContentLoaded', function() {
open() }, false);

function open(){
var number = dayNumber();
var html   = number + '.html';
window.open (html,'_self',false);
}
Dion Zac
  • 69
  • 7
  • Thanks so much for the answer.. That did most of the trick but variable number is undefined. i was not getting dayNumber() value in it. I just merged both into function open () and called . It worked. Thanks again – Raju Shetty Feb 14 '17 at 17:14
  • No problem happy to hear I helped! – Dion Zac Feb 15 '17 at 18:41
  • Actually you had to place "return day" in the end of dayNumber function that was my bad. – Dion Zac Feb 15 '17 at 18:43
0

Change your code as below. It will work. getDAYNumber() is a function it will accept the date and returns the day number.

Date.prototype.getDAYNumber = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}

document.addEventListener('DOMContentLoaded', function() {
open() }, false);


function open(){
var daynum = new Date().getDAYNumber();
var htmlPage = daynum+".html";
window.open (htmlPage,'_self',false);
}
Naresh Kumar
  • 938
  • 5
  • 12