I need to turn my DateTime values into universal time format that can be read by javascript and in particular this needs to be done through the millisecond format.
Eg in javascript this would be new Date(1156867200000); for august 29 2006. Combining any of the ToUniversalTime, Ticks, ToFileTimeUtc, didn't result in a correct representation.
There is not a built in function to do this. Luckily I had this same problem a while back. I found the solution somewhere (forget where), but here is the code(in C#):
public double MilliTimeStamp()
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = DateTime.UtcNow;
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
Note that UNIX timestamps (which JS uses) start at 00:00:00 1/1/1970
Sorry for the repost. I reread your post and you actually wanted to find the Timestamp for a given DateTime. A little modification is needed for the code:
public double MilliTimeStamp(DateTime TheDate)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = TheDate.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
olja
Member
151 Points
44 Posts
converting c# datetime utc date to javascript millisecond representation
Nov 12, 2006 06:14 AM|LINK
I need to turn my DateTime values into universal time format that can be read by javascript and in particular this needs to be done through the millisecond format.
Eg in javascript this would be new Date(1156867200000); for august 29 2006. Combining any of the ToUniversalTime, Ticks, ToFileTimeUtc, didn't result in a correct representation.
All help much appreciated,
Olja
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: converting c# datetime utc date to javascript millisecond representation
Nov 12, 2006 06:53 PM|LINK
I'm not 100% sure, but I believe you want to use DateTime.Ticks
Al
My Blog
RoamingLlama
Member
220 Points
40 Posts
Re: converting c# datetime utc date to javascript millisecond representation
Nov 13, 2006 05:25 PM|LINK
There is not a built in function to do this. Luckily I had this same problem a while back. I found the solution somewhere (forget where), but here is the code(in C#):
public double MilliTimeStamp() { DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = DateTime.UtcNow; TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); return ts.TotalMilliseconds; }Note that UNIX timestamps (which JS uses) start at 00:00:00 1/1/1970
Thanks,
Dan
Dan
RoamingLlama
Member
220 Points
40 Posts
Re: converting c# datetime utc date to javascript millisecond representation
Nov 13, 2006 05:35 PM|LINK
Sorry for the repost. I reread your post and you actually wanted to find the Timestamp for a given DateTime. A little modification is needed for the code:
public double MilliTimeStamp(DateTime TheDate) { DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = TheDate.ToUniversalTime(); TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); return ts.TotalMilliseconds; }Thanks
Dan
Dan
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: converting c# datetime utc date to javascript millisecond representation
Nov 13, 2006 07:18 PM|LINK
Al
My Blog
RoamingLlama
Member
220 Points
40 Posts
Re: converting c# datetime utc date to javascript millisecond representation
Nov 13, 2006 07:21 PM|LINK
Al,
Yes you can use this code in your blog. Thanks fro the compliment.
Dan
Dan
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: converting c# datetime utc date to javascript millisecond representation
Nov 13, 2006 07:37 PM|LINK
Dan,
Thanks, will be up on the 15th or 16th, I added a link to your profile, if there is another link you prefer, let me know.
Cheers
Al
Al
My Blog
olja
Member
151 Points
44 Posts
Re: converting c# datetime utc date to javascript millisecond representation
Dec 21, 2007 04:35 PM|LINK
It's been a while, but I thought I'd add a very belated thank you post anyway!
Olja