Question

I want to convert datetime format for my radtime picker. I am getting 2012-8-2-13-00-00 as my output when I pick date from my radtime picker. When I try to convert in to date it is saying invalid date.

The JavaScript:

function SourqtyValidation()
{
var specifiedtime = document.getElementById('<%=txtSpecifiedArrvialTime.ClientID %>').value;
                alert(specifiedtime);
                var a = new Date(specifiedtime);
var actuvalarrivaltime = document.getElementById('<%=txtActualArrivalTime.ClientID %>').value;
alert(actuvalarrivaltime);
                var b = new Date(actuvalarrivaltime);
                b.format("dd/MM/yyyy hh:mm:ss");
                alert(b);
                var difference =Math.round((a - b) / 1000);
                alert(difference);
}

The aspx:

//txtSpecifiedArrvialTime = predifened as 2012/08/02 09:35:55;

  <telerik:RadTimePicker ID="txtActualArrivalTime" runat="server" EmptyMessage="Actual Arrival Time">                                                        </telerik:RadTimePicker>

So how can I get difference between two times in minutes?

Was it helpful?

Solution

to get the time difference,date should be in same format,
"2012-8-2-13-00-00" is not the correct format, convert it into format of "2012/08/02 13:00:00" than you can get the differencein in second by dividing it to 1000.

you can use this to convert your string to datetime

 var dt = '2012-8-2-13-00-00'.split(/\-|\s/);
    dat = new Date(dt.slice(0,3).reverse().join('/')+' '+dt[3]+':'+dt[4]+':'+dt[5]);

OTHER TIPS

Try using Datejs it has very handy methods:

Eg.:

Date.parse('Thu, 1 July 2004 22:30:00');

Date.parseExact("10/15/2004", "M/d/yyyy");  // The Date of 15-Oct-2004

Very Good library for handling Data Time in javascript.

Datejs is an open-source JavaScript Date Library.

Datejs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top