
We are currently developing a custom Web UI backed by a set of .NET web services. For transfer of date values, we are using millisecond values as returned by the Java and JavaScript Date.getTime() methods. We chose this format because it is easy to transfer and it is native to both JavaScript and Java (we’re planning to port the application to Java as well).
The Java and JavaScript Date.getTime() methods return the number of milliseconds since 1 Jan 1970 00:00:00 GMT. Since .NET represents dates in Ticks (1 Tick = 0.1 nanoseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT, we must use a conversion formula where 621355968000000000 is the offset between the base dates in Ticks and 10000 the number of Ticks per Millisecond.
Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000
Here is an example of how to implement those formulas in C# – a Date Conversion Utility (Note that C# already provides the number of ticks per millisecond as constant – TimeSpan.TicksPerMillisecond):
namespace Qat.Util
{
public static class DateConverter
{
private const long TicksToMillisOffset = 621355968000000000;
public static DateTime MillisecondsToTicks(long ms)
{
return new DateTime((ms * TimeSpan.TicksPerMillisecond)
+ TicksToMillisOffset);
}
public static long TicksToMilliseconds(DateTime dt)
{
return (dt.Ticks - TicksToMillisOffset)
/ TimeSpan.TicksPerMillisecond;
}
}
}
The millisecond values can be used in JavaScript as follows:
// create new Date from milliseconds var d = new Date(milliseconds); // retrieve milliseconds from Date object var millis = d.getTime();
Anke
Written by: Anke Doerfel-Parker |
Connect with us: