0

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.

3
  • 1
    use Date.parse to convert your date string to a Date. Commented Nov 20, 2013 at 9:28
  • Hi. Thanks for your reply. Does Date.Parse() include the Time as well ? also, is it the same as c# ticks ? Commented Nov 20, 2013 at 9:30
  • Ok. So with some research. Date.parse() returns an epoch time, which is not the same as c# ticks. but can be easily converted. :) Commented Nov 20, 2013 at 10:03

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.