How to convert Javascript datetime to C datetime?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A JavaScript Date value and a C# DateTime are not the same wire format. The reliable way to move a date from browser code to .NET is to serialize it into a neutral representation, usually an ISO 8601 string or a Unix timestamp, and then parse it on the server.
The most important decision is whether you want to preserve a time zone offset. If you do, DateTimeOffset is usually the safer .NET type than plain DateTime.
Use ISO 8601 as the Default Format
JavaScript already knows how to serialize a date into a standard format that .NET understands well:
A sample output looks like 2026-03-07T18:30:00.000Z. The trailing Z means UTC.
On the C# side, parse that string with DateTimeOffset.Parse or DateTimeOffset.TryParse:
This round-trip is dependable because both environments agree on the format. It also avoids culture-specific parsing issues such as 03/07/2026 meaning different things in different locales.
Sending Dates from a Browser to an ASP.NET API
In a real application, you usually send JSON rather than passing raw strings around by hand.
Then receive it in C# with a model type that preserves the offset information:
If your API framework is configured normally, the JSON serializer will parse the ISO 8601 value automatically. That is usually cleaner than parsing strings manually in controller code.
When to Use DateTime Instead of DateTimeOffset
DateTime still works when you only care about a clock value and are disciplined about storing everything in UTC. For example:
This produces a UTC DateTime. The problem is that DateTime can also represent Local and Unspecified values, so teams often lose track of what a value means. DateTimeOffset makes intent more obvious when data crosses process or network boundaries.
Using Unix Timestamps
Sometimes you do not want strings in your payload. In that case, send milliseconds since the Unix epoch.
Parse that in C# like this:
Unix timestamps are compact and language-neutral, but they are less readable in logs and harder to inspect manually than ISO strings.
Preserve Time Zone Intent
A common source of bugs is taking a local browser date and assuming the server will interpret it the same way. Consider this JavaScript code:
The first string is shown in the browser's local zone. The second is converted to UTC. If the original meaning was "9 AM in the user's local zone," you should decide whether the server should store the instant in UTC, the original local time, or both.
For scheduling and user-facing calendars, that distinction matters a lot. For audit logs, storing UTC is usually enough.
Common Pitfalls
- Sending locale-formatted strings such as
03/07/2026 6:30 PMand expecting .NET to parse them consistently. - Using
DateTimeeverywhere without checking whether the value isUtc,Local, orUnspecified. - Creating a JavaScript date from a local string and forgetting that
toISOString()converts it to UTC. - Storing Unix seconds on one side and reading them as Unix milliseconds on the other.
- Parsing dates manually in controller code when the JSON serializer could bind them directly to
DateTimeOffset.
Summary
- Convert JavaScript dates to a neutral wire format before sending them to C#.
- Prefer ISO 8601 with
toISOString()for readability and compatibility. - Use
DateTimeOffsetin .NET when you want to preserve offset-aware values. - Use Unix timestamps only when a numeric format is preferable for your API.
- Make time zone intent explicit, especially for scheduling and user-facing features.

