Convert.Changetype converts data incorrectly

Asked

Viewed 145 times

3

The method Convert.ChangeType is converting the date the wrong way I hope 24/02/2015 and comes 23/02/2015. Does anyone know why?

Code example:

using System;
using System.Globalization;

namespace ConsoleApplication4
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string data = @"2015-02-24T01:00:01-01:00";

            Console.WriteLine(DateTime.ParseExact(data, "yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture));//CONVERTE ERRADO
            Console.WriteLine(DateTime.Parse(data));//CONVERTE ERRADO


            Console.WriteLine((DateTime?)Convert.ChangeType(data, Nullable.GetUnderlyingType(typeof(DateTime?))));//CONVERT ERRADO
            Console.WriteLine((DateTime)Convert.ChangeType(data, typeof(DateTime)));//CONVERT ERRADO

            Console.ReadLine();

        }
    }
}

O que meu console escreve

  • 1

    You wait 24/02/2015 and is returning 24/02/2015, it’s not right?

  • Fixed. Sorry I’m at work I made it running.

  • You’re right: https://dotnetfiddle.net/3Fd28k

  • @bigown, I edited the question to show the result of my console. Have any idea?

  • For the time being, I switched to test other cultures but it’s still ok. It’s likely you have something with your environment. I doubt that a different version of . Net is producing different results. https://dotnetfiddle.net/8bfSTH

  • I found this format in Nfe XML. Unfortunately I can’t find more date examples in this format. There is a standard format for XML date?

  • Not that I know, in theory each one can create the format you want. It has a format for the XML definition. But the problem is not format, it seems to be ok, so much so that I showed it works. I would need to figure out why yours . Net is not doing the same.

Show 3 more comments

1 answer

0


I found!!!

Next date comes with that part: -01:00 which means Timezone. When we convert a date it adapts to the PC Timezone.

The program converted to the value: 24/02/2015 01:00:01 in Timezone -1, in my case, I am in SP and Timezone is -3 he subtracted 2h and keeps the value 23/02/2015 23:00:01.

  • Of course. I told you it was because of your environment. It has to do with this: http://answall.com/a/46489/101

  • 1

    Accept the answer so the question is not open :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.