Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Formatting date and time - JavaScript

Hi experts this follows on from an earlier question:
https://www.experts-exchange.com/questions/28697687/Combining-Date-and-Time-in-JavaScript.html
I am starting with a date formatted dd/mm/yyyy and trying to reformat the string to yyyy-mm-dd as follows:

 
var startdate = document.getElementById('<%=TxtStartDate.ClientID%>').value;
 var startparts = startdate.split('/');
 var mystartdate = new Date(startparts[2] + "-" + startparts[1] + "-" + startparts[0]);

Open in new window

then displaying the result to check:

if (startdate != '') {
                document.getElementById('<%=Txttest1.ClientID%>').value = mystartdate;
                
            }

Open in new window


The result I get for 23/07/2015 however is:

Thu Jul 23 01:00:00 UTC+0100 2015

Can anyone advise please?
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>> var mystartdate = new Date(startparts[2] + "-" + startparts[1] + "-" + startparts[0]);
Change that to:
var mystartdate = startparts[2] + "-" + startparts[1] + "-" + startparts[0];

Open in new window


just concatenate the date parts.
You can try something like this - however your sample date is incorrect. If you are going to use that format for date it should be mm/dd/yyyy. Consider 04/04/2015 - it is ambiguous as to what is the month and what is the day. Convention is if you use forward slashes it is US format of m/d/y and if you use - (hyphens) it is UTC (y-m-d).

This code formats the date as you want it - you can modify it as needed.

Date.prototype.ymd= function()
{
    return this.getFullYear().toString() +
        "-" +
        (("00" + (this.getMonth() +1)).substr(-2)) + "-" +
        (("00" + this.getDate()).substr(-2));
};

var testdate = new Date("03/27/2015");
alert(testdate.ymd());

Open in new window


Sample here
Avatar of forsters

ASKER

Ahhh - thanks that worked (so easy when you know how) - I'm wondering if I just made a new problem for myself though...

you'll see from the code below that I am trying to reformat two datetimes to calc the duration in hh:mm between the two; I get a 'difference' result of NaN - presumably because these are now just strings - is there a solution?

var startdate = document.getElementById('<%=TxtStartDate.ClientID%>').value;
            var enddate = document.getElementById('<%=TxtEndDate.ClientID%>').value;
            
            var starthours = document.getElementById('<%=DDLStartHrs.ClientID%>').value;
            var startmins = document.getElementById('<%=DDLStartMins.ClientID%>').value;
            
            var endhours = document.getElementById('<%=DDLStopHrs.ClientID%>').value;
            var endmins = document.getElementById('<%=DDLStopMins.ClientID%>').value;
           

            var startparts = startdate.split('/');
            var endparts = enddate.split('/');
            

            var mystartdate = startparts[2] + "-" + startparts[1] + "-" + startparts[0];
            var myenddate = endparts[2] + "-" + endparts[1] + "-" + endparts[0];
            //var myenddate = new Date(endparts[2] + "-" + endparts[1] + "-" + endparts[0]);
            //var UTCstart = mystartdate.toUTCString();
            //var UTCend = myenddate.toUTCString();
            var started = (mystartdate + 'T' + starthours + ':' + startmins + ':00.000Z')
            var ended = (myenddate + 'T' + endhours + ':' + endmins + ':00.000Z');
            var difference = (ended - started) / (1000 * 3600);
            
            if (startdate != '') {
                document.getElementById('<%=Txttest1.ClientID%>').value = started;
                
            }

            if (enddate != '') {
                document.getElementById('<%=Txttest2.ClientID%>').value = ended;
            }

            if (startdate != '' && enddate != '') {
                document.getElementById('<%=Txttest3.ClientID%>').value = difference;
            }

Open in new window

hi Julian,

I take your point re. date format but dd/mm/yyyy is the format generated by the calendar control.
Struggling to follow your example - sorry maybe my reply to hielo will clarify where I'm at..
Here is a documented version.
// EXTEND THE DATE OBJECT BY ADDING A NEW FUNCTION
// CALLED ymd
Date.prototype.ymd= function()
{
    // RETURN A STRING MADE UP OF 
    // THE FULL YEAR CONCATENATED TO ...

    return this.getFullYear().toString() +

    // THE SEPARATOR, CONCATENATED TO ...
        "-" +
    // THE MONTH. A SIMPLE TRICK TO GET 0 PADDED STRING
    // CONCATENATE THE MONTH (WHICH IS ZERO BASED
    // SO WE HAVE TO ADD 1) AND THEN TAKE THE RIGHT 2
    // CHAR'S  CONCATENATED TO ...
        (("00" + (this.getMonth() +1)).substr(-2)) + 

    // THE SEPARATOR, CONCATENATED TO ...
        "-" +

    // THE DAY. getDate RETURNS THE DAY (1-31)AND WE USE THE SAME 
    // TRICK FOR PADDING WITH ZERO'S
        (("00" + this.getDate()).substr(-2));
};

// CREATE A NEW DATE
var testdate = new Date("03/27/2015");

// OUTPUT IT IN yyyy-mm-dd FORMAT WITH THE NEW FUNCTION
alert(testdate.ymd());

Open in new window

>> var difference = (ended - started) / (1000 * 3600);
What "difference" type are you looking for? Just hours?  what if it exceeds 24?
thanks both- I am looking for a 'difference' of hh:mm - it will not exceed 24hrs in this scenario.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fab thanks so much - that's working