Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.48/5 (6 votes)
Hey there,
so I am getting the following value ( which is indeed C# DateTime.Ticks ) 635556672000000000 to a Date object in javascript?

I've been googling for quite a bit but wasn't able to find an answer that helps me.

So I am trying my luck here :)

Thank you!
Posted
Updated 10-Dec-19 17:59pm
Comments
BillWoodruff 2-Jan-15 14:37pm    
Don't posts like this help:

http://stackoverflow.com/a/10109071/133321
Angelika S Michel 2-Jan-15 14:42pm    
No, because it's not working with DateTime.Ticks and I do not have access to the server application.
Afzaal Ahmad Zeeshan 2-Jan-15 15:02pm    
Angelika, what Bill has said is right. You can directly pass this Ticks amount to the Date() constructor and create a client-side JS object to indicate a dateTime.
Angelika S Michel 2-Jan-15 15:05pm    
No. It's not working. Try it yourself.

var test= new Date(635556672000000000);
test
Invalid Date
Niels H. 5-Apr-22 7:43am    
you need to divide the ticks from C# with 1000 before you do the convert

You'll need the help of an ASP.NET WebService / MVC / Web Form or the like that can pass back to your JavaScript the current time; either through page code itself, or through an AJAX request.

Here's some C# code that translates a ticks integer to a microtime timestamp:


C#
DateTime someDate = new DateTime(635556672000000000); // or whatever variable has your ticks
Int32 unixTimestamp = (Int32)(someDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
unixTimestamp = unixTimestamp * 1000;


Once you've set something up so that you can get the value of unixTimestamp out of an ASP.NET resource, just call it via jQuery or the like. For example, supposing you create an ASP.NET web handler, named time.ashx, that spits out the microtime stamp as plaintext:

JavaScript
var myTimestamp = new Date(); //set default date to now
$.get("http://www.example.com/time.ashx", function() { }).done(function(data) {
//update date with data from get request
myTimestamp = new Date(data);
});


Or something similar. Point is: ASP.NET helper to convert ticks to microtime.
 
Share this answer
 
Comments
Angelika S Michel 2-Jan-15 15:20pm    
Sorry but this is bogus. UnixTimestamp is not a unix timestamp with the *1000 and like I said, I do not have access to serverside applications.
Doug Vanderweide 2-Jan-15 15:41pm    
You asked for JavaScript. JavaScript uses microtime, which is the Unix epoch in milliseconds. GIYF.

If you don't have access to server-side applications, you can't do what you want.
After thinking about this some, it turns out you can do math to convert ticks to microtime in JavaScript alone.

JavaScript
var ticks = 635556672000000000; 

//ticks are in nanotime; convert to microtime
var ticksToMicrotime = ticks / 10000;

//ticks are recorded from 1/1/1; get microtime difference from 1/1/1/ to 1/1/1970
var epochMicrotimeDiff = 2208988800000;

//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);


tickDate is now the date reflected by your ticks timestamp.

http://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx[^]

Also, protip: Try to be more pleasant, you'll find people are more willing to help you. I, for one, have no plans to assist you again.
 
Share this answer
 
v2
I am using ticks-to-date npm package
https://www.npmjs.com/package/ticks-to-date
 
Share this answer
 
v2
Better late than never, I guess?

Here's your solution, completely in js.

http://stackoverflow.com/questions/15486299/convert-c-sharp-net-datetime-ticks-to-days-hours-mins-in-javascript

Enjoy :-)
 
Share this answer
 
var ticks = 635556672000000000; 
 
//ticks are in nanotime; convert to microtime
var ticksToMicrotime = ticks / 10000;
 
//ticks are recorded from 1/1/1; get microtime difference from 1/1/1/ to 1/1/1970
var epochMicrotimeDiff = Math.abs(new Date(0, 0, 1).setFullYear(1));
 
//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);


According to this page (JavaScript setFullYear() Method[^]) the setFullYear method returns "A Number, representing the number of milliseconds between the date object and midnight January 1 1970"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900