Wafer
Wafer

Reputation: 188

Why is Convert.ToDateTime() not working in this example?

I'm trying to use both System.DateTime.Now.ToString() and Convert.ToDateTime and was running into some strange behavior. I have narrowed down the problem to the Convert.ToDateTime. For some reason a DateTime type set with System.DateTime.Now is not the same as one that has been converted from a string. However when you output either of them they appear to be the same.

(I have tried using Trim(), TrimStart(), and TrimEnd() to no avail.)

This is the output in console after running this in unity: https://i.sstatic.net/VWsmB.jpg

using UnityEngine;
using System;

public class DateTimeTest : MonoBehaviour {
    void Start () {
        //Save current time as a DateTime type
        DateTime saveTime = System.DateTime.Now;
        //Save above DateTime as a string
        string store = saveTime.ToString();
        //Convert it back to a DateTime type
        DateTime convertedTime = Convert.ToDateTime(store);

        //Output both DateTimes
        Debug.Log(saveTime + "\n" + convertedTime);

        //Output whether or not they match.
        if (saveTime == convertedTime)
            Debug.Log("Match: Yes");
        else
            Debug.Log("Match: No");

        //Output both DateTimes converted to binary.
        Debug.Log(saveTime.ToBinary() + "\n" + (convertedTime.ToBinary()));
    }
}

Upvotes: 4

Views: 1649

Answers (3)

Yacoub Massad
Yacoub Massad

Reputation: 27871

You lose a lot when you convert a DateTime to a string via DateTime.ToString().

Even if you include the milliseconds like this:

DateTime convertedTime =
    new DateTime(
        saveTime.Year,
        saveTime.Month,
        saveTime.Day,
        saveTime.Hour,
        saveTime.Minute,
        saveTime.Second,
        saveTime.Millisecond);

you would still get a different DateTime that is not equal to the original one.

The reason for this is that internally a DateTime stores a number of ticks (since 12:00:00 midnight, January 1, 0001). Each tick represents one ten-millionth of a second. You need to get the same number of Ticks for the two DateTime objects to be equal.

So, to get an equal DateTime, you need to do this:

DateTime convertedTime = new DateTime(saveTime.Ticks);

Or if you want to convert it to a string (to store it), you can store the ticks as a string like this:

string store = saveTime.Ticks.ToString();

DateTime convertedTime = new DateTime(Convert.ToInt64(store));

Upvotes: 8

Jakub Lortz
Jakub Lortz

Reputation: 14894

The result of DateTime.ToString() does not include milliseconds. When you convert it back to DateTime, you basically truncate the milliseconds, so it returns a different value.

For example

var dateWithMilliseconds = new DateTime(2016, 1, 4, 1, 0, 0, 100);  
int beforeConversion = dateWithMilliseconds.Millisecond;  // 100

var dateAsString = dateWithMilliseconds.ToString();       // 04-01-16 1:00:00 AM (or similar, depends on culture)
var dateFromString = Convert.ToDateTime(dateAsString);
int afterConversion = dateFromString.Millisecond;         // 0

Upvotes: 3

René Vogt
René Vogt

Reputation: 43896

I think you are losing your time zone during the ToString() method. So the re-converted DateTime ends up in a different time zone.

Check also the DateTime.Kind property.

Upvotes: 1

Related Questions