I’ve come across the following method in some inherited legacy code. It feels like it should be replace-able with a simple “return DateTime.Now“. However it seems so obvious that I don’t want to make the change in-case I’m missing some hidden intent of this code.
public static DateTime GetTimeStamp()
{
return new DateTime(DateTime.Now.Ticks);
}
My guess is that at the time the implementer mistaken thought that “DateTime.Now” returned a reference rather than a new instance but has anyone ever come across this or know a real reason it could have been implemented this way.
It is the same as DateTime.Now. Getting the ticks from a DateTime and putting it in the constructor will give you the same datetime:
msdn => DateTime(int64 ticks)
However you can lose the time zone awareness from DateTime.Now: