How can I convert a string date to an integer date in JavaScript?

by on November 6, 2009

Q: How can I display the date and time in decimal format using the UTC, using javascript?

1257539234 would be translate to about 3:31PM EST Nov 6, 2009

How can I translate it back to decimal if needed?


6 Responses to “How can I convert a string date to an integer date in JavaScript?”
  1.  

    You will want to use the JavaScript date function either new Date( [date here] ) or date.parse( [date here] ). The key here is that you need to use a date that it supports and the date you are currently showing is not a date format that is supported.

    datejs.com actually has a library that will help you parse the format you provided though.

     
  2.  

    So, I can take 1257539234 seconds sinse January 1 1970 00:00:00 GMT which does come out to the right date. I will have to create my own function but its simply a matter of division, remainders (repeat). However, I would have to create an array of months so this will get hairy in places so I will need my own .js file. I just thought I could use something like document.write(date(“M D Y”, 1257539234); or whatever, but its okay. I am used to creating my own modules. Not many people write the kinds of things that I write. I work live chat, which needs to work with 3 time zones at the same time. So this is needed. Thanks. If you have any suggestions about a simpler way, let me know.

    As for that site, I plugged that number directly and it gave “snafu” as the result lol I never knew that to be a date on the calander or clock heh maybe they should check for input.typeof 😉

     
  3.  

    You are right. I was thinking the purpose of the datejs.com site was to go from “3:31PM EST Nov 6, 2009” to 1257539234 but all it does is convert it to “Friday, November 06, 2009 3:31:00 PM” which is not very useful unless you want to pass it to date.

    The problem I was having was that

    new Date(“3:31PM EST Nov 6, 2009”) was giving me errors so that was not very useful. If, however, you get the date in a format that the date.parse() understands then you can do

    var currentDate = new Date(‘Thu, 1 July 2004 22:30:00 EST’)

    var mili = currentDate.valueOf()

    and you should have what you are looking for.

     
  4.  

    After much cross-referencing of different math, parsing, function and timer systems, I developed a universal module for comparing times. However, I have not yet coded it to tell you what date is 1,002 days, 323 hours into the future. That would be another module but would be easily adaptable. Below is the module I developed and it can handle the date1.valueOf decimal version of the timestamp. With date1.getTimezoneOffset you can find out how many minutes the computer is from GMT. I hope this function is useful to people looking to synchronise 2 or more computers in real-time communication, such as AJAX 🙂

    function DateCompare(FirstMonth, FirstDay, FirstYear, FirstHour, FirstMinute, FirstSecond, SecondMonth, SecondDay, SecondYear, SecondHour, SecondMinute, SecondSecond) {
    date1 = new Date();
    var DiffArray = new Array(10);
    date2 = new Date();
    diff = new Date();

    if (FirstDay) {
    if (!SecondMonth) { SecondMonth = date2.getMonth() + 1; }
    if (!SecondDay) { SecondDay = date2.getDay() + 1; }
    if (!SecondYear) { SecondYear = date2.getFullYear(); }
    if (!SecondHour) { SecondHour = date2.getHours(); }
    if (!SecondMinute) { SecondMinute = date2.getMinutes(); }
    if (!SecondSecond) { SecondSecond = date2.getSeconds(); }

    date1temp = new Date (FirstMonth + “/” + FirstDay + “/” + FirstYear + ” ” + FirstHour + “:” + FirstMinute + “:” + FirstSecond);
    date1.setTime(date1temp.getTime());
    date2temp = new Date (SecondMonth + “/” + SecondDay + “/” + SecondYear + ” ” + SecondHour + “:” + SecondMinute + “:” + SecondSecond);
    date2.setTime(date2temp.getTime());
    diff.setTime(Math.abs(date1.getTime() – date2.getTime()));

    timediff = diff.getTime();
    } else {
    timediff = Math.abs(FirstMonth – date1.valueOf());
    }

    years = Math.floor(timediff / (52 * 1000 * 60 * 60 * 24 * 7));
    timediff -= years * (52 * 1000 * 60 * 60 * 24 * 7);

    weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
    timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

    days = Math.floor(timediff / (1000 * 60 * 60 * 24));
    timediff -= days * (1000 * 60 * 60 * 24);

    hours = Math.floor(timediff / (1000 * 60 * 60));
    timediff -= hours * (1000 * 60 * 60);

    mins = Math.floor(timediff / (1000 * 60));
    timediff -= mins * (1000 * 60);

    secs = Math.floor(timediff / 1000);
    timediff -= secs * 1000;

    DiffArray[“totalseconds”] = diff.getTime() / 1000;
    DiffArray[“numericdate1”] = date1.valueOf();
    DiffArray[“numericdate2”] = date2.valueOf();
    DiffArray[“years”] = years;
    DiffArray[“weeks”] = weeks;
    DiffArray[“days”] = days;
    DiffArray[“hours”] = hours;
    DiffArray[“minutes”] = mins;
    DiffArray[“seconds”] = secs;
    DiffArray[“difference”] = (years + ” years, ” + weeks + ” weeks, ” + days + ” days, ” + hours + ” hours, ” + mins + ” minutes, ” + secs + ” seconds”);

    return DiffArray;
    }

     
  5.  

    very nice job!

     
  6.  
    Picked as best answer

    Ok, tell me if you know of a function I should add into my js file. http://www.ezhousefixes.com/Scripts/time-date.js That handles DateCompare, DateShift and DecimalToNormal dates 😀

    Isnt it strange with a little bit of help, you can generate your own answers? haha so thanks