c# reproduce datetime from ticks - c#

I have a datetime field like
{01/01/0001 00:01:02}
Millisecond = 30 and the Ticks for the above datetime field is
6203000000
The ticks save in the database as an int value which is 62030. I need to reproduce the above date time using the value in the database (62030). So I tried the following.
var data = 62030;
winTime = new DateTime().AddTicks(Convert.ToInt64(data.ToString().PadRight(10, '0')));
var b = winTime.Ticks;
var b = 6203000000. But it returns minute as 10 instead 01, second as 20 instead of 02 and Millisecond as 300 instead of 030.
Can anyone see what I'm doing wrong?

It seems to me that your "ticks 62030" is actually "milliseconds 62030" in which case it's very simple - you just need to multiply by "the number of ticks per millisecond" which is 10,000. You don't need to use DateTime for this at all:
// Note that if you want any significant length of time, you'd expect to get
// the data as a long, not an int
int data = 62030; // Milliseconds
long ticks = data * 10000L;
... and you certainly don't need string conversions. Converting to a string, padding, and then converting back again is a very tortuous and error-prone way of performing multiplication.
Or if you do need a DateTime:
int data = 62030; // Milliseconds
long dateTime = new DateTime(data * 10000L);
I strongly suspect that any DateTime value that early should actually be treated as a TimeSpan though - what's this really meant to represent? If so, it's even easier:
TimeSpan ts = TimeSpan.FromMilliseconds(data);
Date and time concepts are very easy to mix up, and you end up with some very subtle bugs. Personally I'd recommend using my Noda Time project which separates them more than .NET does, but even if you don't use the library it's worth looking at the list of concepts so you can think about them appropriately within .NET too.

Why not just use the DateTime constructor that accepts an Int64 representing ticks, such that:
var dateTimeFromTicks = new DateTime(ticks);

static void Main(string[] args)
{
/* read datetime now */
DateTime dt = new DateTime();
dt = DateTime.Now;
/* datetime convert to tick */
long TickeringTick = dt.Ticks;
Console.WriteLine("Date time now -> " + dt.ToString("yyyy.MM.dd HH:mm:ss.fffffff"));
Console.WriteLine("Converted to Ticks = " + TickeringTick.ToString());
/* convert tick to datetime */
long myTicks = TickeringTick;
DateTime dt2 = new DateTime(myTicks);
Console.WriteLine("Date from ticks :" + dt.ToString("yyyy.MM.dd HH:mm:ss.fffffff"));
Console.ReadLine();
}

Related

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:
http://imgur.com/1ZIdPH4
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()));
}
}
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));
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
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.

Exact c# result of sql datediff

I'm trying to get the number of days (calculated byu datediff) in sql and the number of days in c# (calculated by DateTime.now.Substract) to be the same, but they return different results....
//returns 0
int reso = DateTime.Now.Subtract(expirationDate).Days;
vs
//returns 1
dateDiff(dd,getDate(),ExpirationDate)
In both cases, ExpirationDate is '10/1/2011 00:00:00', and the code and the DB are sitting on the same server. I want the return int to be the same. I suspect I'm missing something stupid... ideas??
dateDiff(dd,getDate(),ExpirationDate) Is doing a days comparison. DateTime.Now.Subtract(expirationDate).Days is doing a date and time
For example
SELECT dateDiff(dd,'10/1/2011 23:59:00' , '10/2/2011') returns one day even when only one minute apart.
If you want the same in C# you need to remove the time component
e.g.
DateTime dt1 = new DateTime(2011,10,1, 23,59,0);
DateTime dt2 = new DateTime(2011,10,2, 0,0,0);
Console.WriteLine((int) dt2.Subtract(dt1.Subtract(dt1.TimeOfDay)));
So in your case it would be something like
DateTime CurrentDate = DateTime.Now;
int reso = CurrentDate.Subtract(CurrentDate.TimeOfDay).Subtract(DateTime.expirationDate).Days;
I haven't tested it but I would not do
DateTime.Now.Subtract(DateTime.Now.Subtract.TimeOfDay)
Because the second call to Now wouldn't be guaranteeing to be the same as first call to Now
In any case Stealth Rabbi's answer seems more elegant anyway since you're looking for a TimeSpan not a DateTime
10/1/2011 is less than 1 day away from DateTime.Now. Since you're getting back a TimeSpan and then applying Days to it, you're getting back a TimeSpan that is < 1 day. So it'll return 0 Days.
Instead, just use the Date component of those DateTimes and it'll correctly report the number of days apart - like this:
DateTime now = DateTime.Now;
DateTime tomorrow = new DateTime(2011, 10, 1);
var val = (tomorrow.Date - now.Date).Days;
This will yield you 1 day.
I'm assuming you want the number of Total days, not the number of days from the largest previous unit. You'd want to use the TotalDays property. Also, you may find it easier to use the minus operator to do a subtraction
DateTime d1 = DateTime.Now;
DateTime d2 = new DateTime(2009, 1, 2);
TimeSpan difference = d1 - d2;
Console.WriteLine(difference.TotalDays); // Outputs (today):1001.46817997424

Parsing times above 24 hours in C#

Suppose a time stamp (just time or date and time) where the time can roll over to the next day:
00:00:00 <- midnight
01:00:00 <- 1 AM
23:00:00 <- 11 PM
24:00:00 <- midnight, day + 1
25:00:00 <- 1 AM, day + 1
What would be a way to parse it easily into a C# DateTime that would perform the carry-over to the next day? In other words, "01:00:00" would become "0001-01-01 01:00:00" and "25:00:00" would become "0001-01-02 01:00:00".
EDIT:
I should mention that this fails miserably (i.e FormatException):
DateTime.ParseExact("0001-01-01 25:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Since you're trying to represent a period of time from an arbitrary point, rather than as a specific date, perhaps you would be better off using the System.TimeSpan class? This allows you to set values of more than 24 hours in the constructor, and can be used with DateTime objects like this:
System.TimeSpan timestamp = new System.TimeSpan(25, 0, 0);
System.DateTime parsedDateTime = new DateTime(0, 0, 0);
parsedDateTime = parsedDateTime.Add(timestamp);
Console.WriteLine(parsedDateTime.ToString("yyyy-MM-dd HH:mm:ss")); //Output as "0001-01-02 01:00:00"
NOTE: Code is untested.
EDIT: In terms of parsing the strings, I can't think of any basic .NET objects that parse strings with values greater than 23 for the hour (since 25 is an invalid hour of the day), but assuming that the format is consistent, you could create a very simple string parsing routine (or even a regular expression) to read the values individually, and load the constructor manually.
If you have an existing DateTime value you can add to, you can always use a TimeSpan:
string dt = "25:00:00";
int hours = int.Parse(dt.Split(':')[0]);
TimeSpan ts = TimeSpan.FromHours(hours);
TimeSpan.Parse() doesn't work directly in this case because it complains (fair enough!) about the 25 in the hour notation.
If you want to code it out... this should be a starting point:
string dateString = "0001-01-01 25:00:00";
string[] parts = dateString.Split(' '); //now have '0001-01-01' and '25:00:00'
string datePart = parts[0]; // '0001-01-01'
string[] timeParts = parts[1].Split(':'); //now have '25', '00', and '00
DateTime initialDate = DateTime.ParseExact(datePart, "yyyy-MM-dd", CultureInfo.InvariantCulture);//use the date as a starting point
//use the add methods to get your desired datetime
int hours = int.Parse(timeParts[0]);
int minutes = int.Parse(timeParts[1]);
int seconds = int.Parse(timeParts[2]);
DateTime resultDate = initialDate.AddHours(hours)
.AddMinutes(minutes)
.AddSeconds(seconds);
Of course, it makes assumptions that the input is formatted properly and is parsable, etc..
In addition, you could definitely use timespan instead of the individual add methods for hour, minute, second as some other answers are..
In case nobody points out an out-of-the-box answer, here is a neat ActionScript class I wrote to parse time inputs (human input)...
https://github.com/appcove/AppStruct/blob/master/Flex/AppStruct/src/AppStruct/TimeInput.as
It would be very simple to port this to C#, and you could tweak the 24 hour logic to result in #days, #hours, #minutes.
Good luck!
You are specifying an invalid date. So not only can you not parse it, you cannot store it!
How about a nice TimeSpan object instead? (It also has a Parse() method.)
Alternatively, use a sscanf()-type function like the one at http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net to extract each number separate. (Best if you have no control over the string format being read.)

Constructing a DateTime one step at a time

I am trying to construct a DateTime in C# one step at a time, as in, the whole date not being in the constructor. I'm not understanding what is wrong though.
DateTime d = new DateTime((long)0);
d.AddYears(2000);
with that d.Years will still be equal to 1 though. Also, I must store the date as a long. So I can't just build the date with a huge constructor and I also can't have a persisting DateTime instance, so I dump it to a long and then restore it and I start with a value of 0. Am I suppose to start with a different value than zero?
what exactly is wrong?
A DateTime structure is immutable, meaning that its properties cannot change.
The AddYears method returns a new DateTime that you must use:
DateTime d = new DateTime((long)0);
d = d.AddYears(2000);
Probably off-topic, but if you need to persist your DateTime as a long then why not persist the value returned by its Ticks property.
You can then restore your DateTime instance by using the constructor that takes a ticks parameter:
// stage 1
DateTime dt = DateTime.MinValue.AddYears(2009);
PersistTicksToSomewhere(dt.Ticks);
// stage 2
long ticks = GetPersistedTicksFromSomewhere();
DateTime dt = new DateTime(ticks).AddMonths(8);
PersistTicksToSomewhere(dt.Ticks);
// stage 3
long ticks = GetPersistedTicksFromSomewhere();
DateTime dt = new DateTime(ticks).AddDays(20);
PersistTicksToSomewhere(dt.Ticks);
// etc etc
There are 12 different overloads for the DateTime constructor. There should be at least one you can adapt for your use.
This:
DateTime d = new DateTime(2000,0,0);
is better than:
DateTime d = new DateTime((long)0);
d = d.AddYears(2000);
Just construct as much of the date as you can up front and put in zeros for the other parameters.
DateTime is immutable so you must change it as so
DateTime d = new DateTime();
d d.AddYears(2000);
However this will instantiate a new DateTime 99.9% of the time this is fine but if it's nested in a loop that runs forever you're better off using one of the many DateTime constructors. Use the same rule of thumb as string and StringBuilder.

Add or Sum of hours like 13:30+00:00:20=13:30:20 but how?

I want to add seconds (00:00:02) or minutes (00:00:20) on datetime value (may be stored string type) but how? Examples:
13:30+02:02:02= 15:32:02 ,
13:30+00:00:01= 13:30:01 ,
13:30+00:01:00=13:31:00 or 13:30 (not important)
Can you help me? I need your cool algorithm :) Thanks again...
myDateTimeVariable.Add(new TimeSpan(2,2,2));
If you choose to use the TimeSpan, be aware about the Days part:
TimeSpan t1 = TimeSpan.Parse("23:30");
TimeSpan t2 = TimeSpan.Parse("00:40:00");
TimeSpan t3 = t1.Add(t2);
Console.WriteLine(t3); // 1.00:10:00
With DateTime:
DateTime d1 = DateTime.Parse("23:30");
DateTime d2 = DateTime.Parse("00:40:00");
DateTime d3 = d1.Add(d2.TimeOfDay);
Console.WriteLine(d3.TimeOfDay); // 00:10:00
Adding two datetimes from strings:
var result = DateTime.Parse(firstDate) + DateTime.Parse(secondDate);
Adding a string time to a datetime:
var result = existingDateTime.Add(TimeSpan.Parse(stringTime);
Adding time as in your example:
var result = TimeSpan.Parse("12:30:22") + TimeSpan.Parse("11:20:22");
Finally, your example as dates (not tested!):
var result = DateTime.Parse("12:30:22") + DateTime.Parse("11:20:22");
Note that this is sloppy coding, but you get the idea. You need to verify somehow that the string is actually parseable.
Not really sure what you're after, but can you not just use the built in functions to C#'s DateTime object?
DateTime myDate = DateTime.Now;
myDate = myDate.AddHours(1);
myDate = myDate.AddMinutes(30);
myDate = myDate.AddSeconds(45);
The problem is more abstract. As already mentioned, in .NET there are two types - DateTime and TimeSpan. The DateTime type represents a specific point in time. It's not an interval of time. It's a specific location in all time since the birth of the Universe. Even if you set the year/month/day components to 0, it will still represent some absolute point in time. Not a length of time.
The TimeSpan on the other hand represents some interval. 1 minute, 2 days, whatever. It's not specified WHEN, just HOW LONG.
So if you were to subtract two DateTime objects you would get a TimeSpan object that specifies how much time there is between them. And if you add a TimeSpan to a DateTime you get another DateTime. But you can't add a DateTime to another DateTime - that would make no sense.
It sounds to me like you should be working with TimeSpans all the time, because you are dealing with lengths of time, not absolute points in time. If you get these lengths from your source as a DateTime then that's actually not correct, and you should convert them to TimeSpans somehow. The parsing method is one way that has been suggested, but you might also try to subtract zero DateTime from it. That might be faster and more culture-independant.
use the TimeSpan structure. you can add TimeSpans together, or you can add a TimeSpan to a DateTime to produce a new DateTime.
You should have a look at TimeSpan.Parse. This converts a string to a TimeSpan object. That way you can do stuff like
TimeSpan a = TimeSpan.Parse(timeStringA)+TimeSpan.Parse(TimeStringB);
To split a string like "00:00:20+00:01:00" look at string.split
stringA = timeSting.split('+')[0];
stringb = timeSting.split('+')[1];
return string.Format("{0}:{1}:{2}", mytimespan.Hours
+ (mytimespan.Days*24),mytimespan.Minutes,mytimespan.Seconds);
static void Main(string[] args)
{
String timeText = "3/23/2015 12:00:13 AM";
String timeText2 = "3/23/2015 1:45:03 AM";
DateTime time = Convert.ToDateTime(timeText);
string temp = time.ToString("HH:mm:ss");
DateTime time2 = Convert.ToDateTime(timeText2);
string temp2 = time2.ToString("HH:mm:ss");
TimeSpan t1 = TimeSpan.Parse(temp);
TimeSpan t2 = TimeSpan.Parse(temp2);
Console.Out.WriteLine(t1 + t2); // 01:45:16
Console.ReadLine();
}

Categories