KapteinMarshall
KapteinMarshall

Reputation: 490

Get C# DateTime Ticks from html <Input> of type DateTime

I was hoping one of you guys might be able to help me. I am a bit stuck and I am very new to javascript.

I need to post a DateTime value (as c# ticks) to a webservice call from a mobile application. The mobile application is written in html/javascript.

Currently I have code that works for a normal Date. It is as follows.

function getTicksFromDatePicker(value) {
    var dateparts = value.split("-");
    //date format(Fullyear,month,date) 
    var startDate = new Date(dateparts[0], (dateparts[1] - 1).toString(), 
              (dateparts[2]).toString()); 
    var ret = ((startDate.getTime() * 10000) + 621355968000000000);
    if (isNaN(ret)) {
        ret = 1;
    }
    return ret;
}

This code works perfectly if I use a normal html < input type="date" > and pass its value to my function.

I need to write a function which takes the value from an input of type="datetime" and convert it into c# ticks. I am having trouble doing so. I googled around but I did not find anything on how to. I would greatly appreciate any help you guys can provide me with.

Upvotes: 0

Views: 865

Answers (2)

KapteinMarshall
KapteinMarshall

Reputation: 490

Ok. So my new solution is as follows. If anyone can provide a better solution, I will be willing to accept it. :)

function toCsharpticks(value)
{
   var dateRet = Date.parse(value);
   dateRet = (dateRet * 10000) + 621355968000000000
   return dateRet
}

There are 621355968000000000 epoch ticks for javascript from Ist Jan 1900 to Ist Jan 1970. And here 10000 are the ticks per milliseconds.

If anyone has a more elegant solution. I am more than happy to accept it instead. :)

Upvotes: 0

Praveen
Praveen

Reputation: 56529

var startDate = new Date(dateparts[0], (dateparts[1] - 1).toString(), 
              (dateparts[2]).toString()); 

This above statement returns only the date and where the time 00:00:00

So what you have to do is to add time to startDate.

Upvotes: 1

Related Questions