Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Parsing a JSON date into a C# DateTime

i have an API that get information from targetProcess and put that information in sql tables.

the issue comes when a targetProcess field is date because i recieved it as json date "/Date(1409202000000-0500 )/".

How can i convert this json date to Datetime in c#?

i have been trying add extra quotes like the asnwer in this StackOverflow Post but it doesnt work.

i tried replace the the word Date for only use 1409202000000-0500 in

DateTime dotNetDate = new DateTime(1970, 1, 1);
dotNetDate = dotNetDate.AddMilliseconds(Convert.ToInt64(1409202000000-0500) 

i use too JSON.NET as bellow

string sa = "/Date(1409202000000-0500 )/"
DateTime dt = new DateTime();
dt = JsonConvert.DeserializeObject<DateTime>(sa);

but it doesnt work too,

so the question is....How can i convert this json date to Datetime in c#?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You need to manually wrap your string "programatically" in quotes to ensure that it properly formatted correctly:

string sa = @"""" + "/Date(1409202000000-0500 )/" + @"""";
DateTime dt = JsonConvert.DeserializeObject<DateTime>(sa);

If you need to call it multiple times (which it seems like you do), just move the wrapping responsibility to a method:

public string WrapStringInQuotes(string input)
{
    return @"""" + input + @"""";
}
about 4 years ago · Santiago Trujillo Denunciar

0

The issue is with your date string. instead of

string sa = "/Date(1409202000000-0500 )/"

try

string sa = @"""/Date(1409202000000-0500)/""";

Change your code :

 string sa = @"""/Date(1409202000000-0500)/""";
            DateTime dt = new DateTime();
            dt = JsonConvert.DeserializeObject<DateTime>(sa);
            // dt = "2014-08-28 3.00.00 PM"
about 4 years ago · Santiago Trujillo Denunciar

0

Here you have my function. There is even a possibility that you can get a shorter number for the Json date like in OpenweatherAPI for sunrise and sunset, then in this case you need to multiplicate with 1000. My function handle both scenarios.

    public static DateTime GetDateTimeFromJSON(long jsonDateTime, bool shorter = false)
    {
        return JsonConvert.DeserializeObject<DateTime>($"\"\\/Date({ (shorter ? jsonDateTime * 1000 : jsonDateTime) })\\/\"", new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
        }).ToLocalTime();
    }
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda