How to Convert a JavaScript Date to .NET Ticks
When working in a mixed environment where a JavaScript front-end communicates with a .NET back-end, you may need to convert a JavaScript Date object into a format that the .NET framework understands natively. One such format is .NET Ticks, a high-precision time measurement.
This guide will explain what .NET Ticks are, teach you the correct formula for converting a JavaScript Date to ticks, and provide a reusable function to perform the conversion. We will also cover the reverse operation for full interoperability.
What are .NET Ticks?
A ".NET Tick" is the fundamental unit of time measurement in the Microsoft .NET Framework. It is a 64-bit integer that represents the number of 100-nanosecond intervals that have elapsed since 00:00:00, January 1, 0001. This starting point is known as the .NET Epoch.
The Core Concepts: Ticks, Epochs, and the Offset
The key to the conversion is understanding the difference between the JavaScript and .NET time systems.
| System | Unit | Epoch (Starting Point) |
|---|---|---|
JavaScript Date | Milliseconds | The Unix Epoch (Jan 1, 1970) |
| .NET Ticks | 100-nanosecond intervals | The .NET Epoch (Jan 1, 0001) |
To convert from a JavaScript Date to .NET Ticks, we must perform two transformations:
- Convert Units: Change the JavaScript milliseconds into 100-nanosecond intervals. (There are 10,000 ticks in 1 millisecond).
- Add the Epoch Offset: Account for the vast time difference between the .NET Epoch (0001) and the Unix Epoch (1970).
The Solution: Converting a Date to Ticks
This function encapsulates the conversion logic and makes it reusable.
Problem: you have a JavaScript Date object and need to send its value to a .NET API that expects a long representing ticks.
// Problem: How to convert this Date object to .NET Ticks?
const myDate = new Date('2023-10-27T10:00:00Z');
Solution:
function dateToTicks(date) {
// The number of ticks for the "Unix Epoch" (January 1, 1970)
const epochOffset = 621355968000000000;
// There are 10,000 ticks in a millisecond.
const ticksPerMillisecond = 10000;
// Get the milliseconds from the Unix Epoch
const jsMillis = date.getTime();
// Convert to ticks and add the epoch offset.
return (jsMillis * ticksPerMillisecond) + epochOffset;
}
// Example Usage:
const myDate = new Date('2023-10-27T10:00:00Z'); // Using UTC for consistency
const ticks = dateToTicks(myDate);
console.log(ticks); // Output: 638339976000000000
Bonus: Converting Ticks back to a Date
For full interoperability, you also need a function to convert a long value for ticks from a .NET API back into a JavaScript Date object. This is simply the reverse of the previous formula.
function ticksToDate(ticks) {
const epochOffset = 621355968000000000;
const ticksPerMillisecond = 10000;
// Subtract the offset and convert back to milliseconds
const jsMillis = (ticks - epochOffset) / ticksPerMillisecond;
return new Date(jsMillis);
}
// Example Usage (proving the logic works both ways):
const originalTicks = 638339880000000000;
const myDate = ticksToDate(originalTicks);
console.log(myDate.toUTCString()); // Output: Fri, 27 Oct 2023 07:20:00 GMT
Practical Example: Preparing Data for a .NET API
A common use case is serializing a JavaScript object to send to a .NET back-end.
function dateToTicks(date) {
const epochOffset = 621355968000000000;
const ticksPerMillisecond = 10000;
const jsMillis = date.getTime();
return (jsMillis * ticksPerMillisecond) + epochOffset;
}
const appointment = {
title: 'Project Kickoff Meeting',
startTime: new Date('2023-11-15T14:00:00Z'),
attendees: ['Alice', 'Bob']
};
// Convert the date to ticks before sending
const dataForApi = {
...appointment,
startTime: dateToTicks(appointment.startTime)
};
console.log(JSON.stringify(dataForApi, null, 2));
Output:
{
"title": "Project Kickoff Meeting",
"startTime": 638356536000000000,
"attendees": [
"Alice",
"Bob"
]
}
This payload is now in a format that a .NET API can easily deserialize.
Conclusion
Converting between JavaScript Date objects and .NET Ticks is a straightforward mathematical operation once you understand the two key differences between the systems.
- Difference in Epoch: JavaScript's
Datestarts from the Unix Epoch (1970), while .NET Ticks start from January 1, 0001. - Difference in Unit: JavaScript measures in milliseconds, while .NET Ticks measure in 100-nanosecond intervals.
The formula for converting a Date to Ticks is:
ticks = (date.getTime() * 10000) + 621355968000000000;
By using this formula, you can ensure seamless interoperability when sending date and time information between JavaScript and .NET applications.