www.webdeveloper.com
Results 1 to 6 of 6

Thread: Date format question

  1. #1
    Join Date
    Sep 2007
    Posts
    22

    Date format question

    This code works in Chrome, but not IE.
    <code>
    DateData = document.form2.D10.value;
    var Date7l = new Date(DateData);
    Date7l.setDate(Date7l.getDate() + 7);
    </code>

    Its takes a date like :1/1/2014 and steps back 7 days to show:
    <date>
    Wed Jan 08 2014 00:00:00 GMT-0500 (Eastern Standard Time)
    </date>

    Adding this line:
    <code>
    var s7 = Date7l.toLocaleString().substring(0, Date7l.toLocaleString().lastIndexOf("/") + 5);
    </code>

    This gives the correct format in chrome, but in IE shoes the first 4 digits of a weekday and no date.

    What can I do to resolve this guys.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,216
    What is it you want to do?

    What you have shown is not stepping back days but going forward.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Sep 2007
    Posts
    22
    Ignore the +5. Problem is, Chrome and IE show dates differently.

    >>
    Chrome: Wed Jan 08 2014 00:00:00 GMT-0500 (Eastern Standard Time)
    IE: Wed Jan 8 00:00:00 EST 2014
    <<
    So that line will not work for me.
    I need to convert the date/time returned to the short date format.
    Hopefully not some long way round sort of coding.

    OMG. I wish JS had the Dateadd function....

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,216
    Date object has many methods, these are

    Code:
    dateObj = new Date();
    
    hours = dateObj.getHours();
    minutes = dateObj.getMinutes();
    seconds = dateObj.getSeconds();
    
    date = dateObj.getDate();
    month = dateObj.getMonth() + 1;
    year = dateObj.getFullYear();
    you could do something like this.

    Code:
    dateObj = new Date();
    dateString = [dateObj.getDate(),"/",(dateObj.getMonth()+1<10?"0":""),dateObj.getMonth() + 1,"/",dateObj.getFullYear()," ",(dateObj.getHours()<10?"0":""),dateObj.getHours(),":",(dateObj.getMinutes()<10?"0":""),dateObj.getMinutes(),":",(dateObj.getSeconds()<10?"0":""),dateObj.getSeconds()].join("");
    You could use the .toUTCString() method and slice out the time element which is preformatted and would make the date / time shorter.

    dateString = [dateObj.getDate(),"/",(dateObj.getMonth()+1<10?"0":""),dateObj.getMonth() + 1,"/",dateObj.getFullYear(),dateObj.toUTCString().slice(16,25)].join("");


    or another way of making a date / time stamp would be
    Code:
    dateObj = new Date();
    timeStr = dateObj.toUTCString().slice(16,25)
    tmpStr = dateObj.toUTCString().split(" ");
    tmpStr.shift(); 
    date = tmpStr.shift();
    tmpStr.shift();
    months = (dateObj.getMonth()+1<10?"0":"") + dateObj.getMonth() + 1;
    years = tmpStr.shift();
    dateStr = [date,months,years].join("/");
    dateStr+= timeStr;
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    You can use these functions:

    Code:
    var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    var days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    
    function DateAdd(originalDate,millisecondOffset) {
    // Returns new date object from offset
    return new Date(originalDate.getTime()+millisecondOffset);
    }
    
    function ConvertDateToString(dateObject) {
    var NewYear = dateObject.getFullYear();
    var NewMonth = months[dateObject.getMonth()];
    var NewDate = dateObject.getDate();
    var NewDay = days[dateObject.getDay()];
    return NewDay+' '+NewMonth+' '+NewDate+' '+NewYear;
    }
    
    // Usage:
    var FromDate = new Date(); // This is today's date
    // You can also use it like "new Date(year, month, day);" or "new Date(milliseconds);" to specify a date other than the current date
    var GoBackByDays = 7;
    var NewDateObject = DateAdd(FromDate,1000*60*60*24*GoBackByDays*-1);
    var DateStringOutput = ConvertDateToString(NewDateObject);
    Please see http://www.w3schools.com/jsref/jsref_obj_date.asp for more information.
    Last edited by Gray1989; 01-24-2014 at 09:54 PM.

  6. #6
    Join Date
    Mar 2012
    Posts
    202
    Sorry, a little sloppy. Revised Code:

    Code:
    function DateAdd(originalDate,dateOffset) {
    // Returns new date object from offset
    return new Date(originalDate.getTime()+dateOffset*1000*60*60*24);
    }
    
    function ConvertDateToString(dateObject) {
    var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    var days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    var NewYear = dateObject.getFullYear();
    var NewMonth = months[dateObject.getMonth()];
    var NewDate = dateObject.getDate();
    var NewDay = days[dateObject.getDay()];
    return NewDay+' '+NewMonth+' '+NewDate+' '+NewYear;
    }
    
    // Usage:
    var FromDate = new Date(); // This is today's date
    // You can also use it like "new Date(year, month, day);" or "new Date(milliseconds);" to specify a date other than the current date
    var NewDateObject = DateAdd(FromDate,-7);
    var DateStringOutput = ConvertDateToString(NewDateObject);
    Last edited by Gray1989; 01-24-2014 at 10:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles