0

I'm using .NET Core and want to serialize a date into the same format as the System.Web.Script.JavascriptSerializer, but using Newtonsoft jsonconverter instead (or something else compatible with .NET Core since the JavascriptSerializer is .NET framework).

Example:

DateTime result1;
var dt1 = DateTime.TryParse("12.06.2012 10:34:00",CultureInfo.GetCultureInfo("DA-dk"), DateTimeStyles.None, out result1);

JsonConvert.Serialize(result1);

This does NOT return a format like this that I need: "/Date(1249335477787)/";

How can I get a date like this with .NET Core

Thanks

8
  • If you can handle getting timezone delta added, like "\/Date(1560241243104+0200)\/", then the way to do that is to specify a JsonSerializerSettings object where you set settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; Commented Jun 11, 2019 at 8:21
  • If you serialize a DateTime with Kind=DateTimeKind.Utc, you don't get the +0200. Commented Jun 11, 2019 at 8:22
  • Why do you want to do that at all? You should probably fix whatever code uses that format to work with the standard. The defacto standard for dates in JSON is to use ISO8601. JavascriptSerializer is obsolete, for quite a few years now. Even ASP.NET Web API uses Json.NET and ISO8601 Commented Jun 11, 2019 at 10:50
  • Thanks @LasseVågsætherKarlsen. Kanavos: Apparently I need to use the "old" MicrosoftDateFormat to create a new azure automation job with datetime parameters. So I'm pretty much in the hands of the format MS expects here. I don't know if this is becaue the runbook is based on powershell or just the way the API works. Commented Jun 11, 2019 at 10:56
  • @Morten_564834 that's the thing. MS doesn't expect that format. Which service are you trying to call? Commented Jun 11, 2019 at 11:08

1 Answer 1

2

The default format for serializing dates that JSON.NET uses is ISO 8601 which is properly understood by the majority of parsers and languages (including JavaScript). In the past, the format that you know from the JavascriptSerializer has been used. If you need to use that format, then you can configure it through the DateFormatHandling configuration.

In ASP.NET Core 2.x, you can configure it like this within the ConfigureServices method in your Startup class:

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
});

Starting with ASP.NET Core 3.0, a different serializer is used by default which will not have this configuration option, but you can choose to switch back to JSON.NET there too and configure it accordingly:

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
    });

You will need a reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson then though.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I will try that. Do you know if the new Core 3.0 serializer has the same kind of option? Also I'm using this to start Azure automation runbook jobs, and apparently microsoft expect the format to be in this MicrosoftDateFormat (and not ISO 8601). So I guess they are using an old standard still :)
@Morten_564834 The new JSON serializer will use the O format by default. There currently does not exist a way to change that. So you won’t be able to fall back to that Date(timestamp) format with that serializer. Are you sure that the Azure thing doesn’t also just support accept ISO dates? It would be rather odd to think that it would require something that was very .NET specific back then.
Alright thanks. Yea I tried the o format and s format, tried hardcoding the format etc. to see if it had a fallback format. But seems like the .NET is the only one. But this is also very specific to a parameter input to adapt to a powershell [datetime] parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.