Michael
Michael

Reputation: 672

Unrecognized Date format

Good !

I am having some difficulties with extracting data from a date. The thing is that I get a number from an undocumented API.

"created": 734394
"last_chapter_date": 734883

I tried dividing it by 365,242 days (exact amount of days a year)

2010,705231052289
So apparently these are the number of days passed since 0.0.0000

I am currently trying something like that: http://jsfiddle.net/LRUy5/4/

function zero21970(nDays) {
    // 0     70   2013  
    // |-----|-----|  

    // 0 to date
    var dateMils = nDays*24*60*60*100;
    // 0 to 1970
    zeroTo1970 = (1970*365.242)*24*60*60*100;

    //subtract time from 0-1970 from the time 0-date
    //to cut out the part from 1970-today
    return new Date(dateMils-zeroTo1970);
}

//http://www.mangaeden.com/api/manga/4e70e9f6c092255ef7004344/
zero21970(734394) //-> Jan 26 1974

I need to save it in a database and work with it via php or javascript.. Does anyone recognize this kind of format or do you know a convenient way of formatting it?

Edit: I should add that the last chapter came out around 15.01.2013.. just to have something to grab.

Upvotes: 1

Views: 355

Answers (2)

VisioN
VisioN

Reputation: 145388

Updated version:

I guess if the last chapter was from 2013, then the value is a number of days from 01.01.0001. So we can update the initial date as well as change setHours to setDate method for more accuracy:

var date = new Date("0001");
date.setDate(734883);
date.toGMTString();  // "Tue, 15 Jan 2013 00:00:00 GMT"

DEMO: http://jsfiddle.net/LRUy5/6/


Old version:

I found one solution that successfully works at my computer:

var date = new Date("0000");
date.setHours(734394 * 24);
date.toGMTString();  // "Mon, 13 Sep 2010 21:00:00 GMT"

DEMO: http://jsfiddle.net/LRUy5/5/

Upvotes: 3

user1881928
user1881928

Reputation: 616

If you're using PHP, then you should replace

return new Date(dateMils-zeroTo1970);

with

return date('Y-m-d', (dateMils-zeroTo1970));

Upvotes: 0

Related Questions