combine parts from 2 different datetime variables into a new datetime variable - c#

I have 2 datetime objects that I need to combine.
This contains the correct date, but the time part is not needed.
DateTime? sessionDate = fl.EventDateTimeStart
This contains the correct time, but the date part needs to be the date value from the
sessionDate object above.
DateTime? sessionStartTime = g.GameStartTime.Value
I tried using some of the various DateTime toString() methods, but found out that
because they are part of a class, they need to remain DateTime? types
so I can't just convert them to a string.
So I came up with this really ugly method:
sessionStartTime = new DateTime(
fl.EventDateTimeStart.Value.Year,
fl.EventDateTimeStart.Value.Month,
fl.EventDateTimeStart.Value.Day,
g.GameStartTime.Value.Hour,
g.GameStartTime.Value.Minute,
g.GameStartTime.Value.Second)
Is there a more elegant way of to do this?
Thanks!

Sure.
var result = fl.Value.Date + g.Value.TimeOfDay;
DateTime.Date returns a DateTime with the time part set to midnight. DateTime.TimeOfDay gets a TimeSpan containing the fraction of the day that has elapsed since midnight.
Make sure that both of your DateTimes have the same Kind, otherwise the result might not be what you expect.

Related

C# actual DateTime - 14

I want to have a the DateTime 14 days ago.
In C# you only can add days...
I tried this but it doesn't work:
DateTime daysToKeep = DateTime.Now;
daysToKeep.AddDays(-14);
thx.
You must assign the result:
DateTime daysToKeep = DateTime.Now;
daysToKeep = daysToKeep.AddDays(-14);
The AddDays() method does not modify the object itself.
DateTime daysToKeep = DateTime.Now.AddDays(-14);
You need to stress some concepts here.
DateTime Objects (Like String) are immutable, you can't change them. So their methods return new Objects and not change the object itself.
From MSDN:
When working with the DateTime structure, be aware that a DateTime
type is an immutable value. Therefore, methods such as AddDays
retrieve a new DateTime value instead of incrementing an existing
value. The following example illustrates how to increment a DateTime
type by a day using the statement dt = dt.AddDays(1).

How to merge two nullable DateTime objects one contaning time other date I need into one object?

I've got two DateTime objects. The FetchTime contains time hh:mm:ss I am insterested in, FetchDate contains date: year, month, day.
Example:
Debug.WriteLine("Time " + FetchTime);
Debug.WriteLine("Date " + FetchDate);
displays for example:
Time 2014-09-10 23:04:00
Date 2014-09-15 00:00:00
and I would like to get DateTime object which looks like that:
2014-09-15 23:04:00
I would like to merge those two into one or modify one of them. I thought it would be easy but I can't see almost any methods for DateTime object. Is it achievable or first I must convert DateTime to another type then convert it back? Finally, I have to have DateTime object because it is going to be added to SQL database .
EDIT:
I refer to nullable DateTime: DateTime? FetchDate, DateTime? FetchTime.
Just use the DateTime constructor
var date = new DateTime(FetchDate.Year, FetchDate.Month, FetchDate.Day,
FetchTime.Hour, FetchTime.Minute, FetchTime.Second);
Update: It seems like you are using nullable DateTime. So you should get the underlying DateTime value using Value property.
var fetchDate = FetchDate.Value;
var fetchTime = FetchTime.Value;
var date = new DateTime(fetchDate.Year, fetchDate.Month, fetchDate.Day,
fetchTime.Hour, fetchTime.Minute, fetchTime.Second);
Whenever you use the ? suffix on a value type you will always need to call the .Value member before you can call any members on your type.
The ? suffix is actually a special symbol for the compiler to transform your type reference from T? to Nullable<T>. For additional details see the MSDN documentation for Nullable`1.
For your example you will want to use the following to merge the values together.
new DateTime(
FetchDate.Value.Year,
FetchDate.Value.Month,
FetchDate.Value.Day,
FetchTime.Value.Hour,
FetchTime.Value.Minutes,
FetchTime.Value.Seconds)
Make sure to check for null using == null or .HasValue if you are concerned about null values, otherwise you will encounter a System.InvalidOperationException.
For fun, here is another possible way:
DateTime date = new DateTime(2014, 9, 15);
DateTime datetime = new DateTime(2014, 9, 10, 23, 4, 0);
DateTime combined = date + datetime.TimeOfDay;

DateTime.Now - first and last minutes of the day

Is there any easy way to get a DateTime's "TimeMin" and "TimeMax"?
TimeMin: The very first moment of the day. There is no DateTime that occurs before this one and still occurs on the same day.
TimeMax: The very last moment of the day. There is no DateTime that occurs after this one and still occurs on the same day.
These values would be helpful for filtering and doing date-related queries.
Here are two extensions I use to do exactly that.
/// <summary>
/// Gets the 12:00:00 instance of a DateTime
/// </summary>
public static DateTime AbsoluteStart(this DateTime dateTime)
{
return dateTime.Date;
}
/// <summary>
/// Gets the 11:59:59 instance of a DateTime
/// </summary>
public static DateTime AbsoluteEnd(this DateTime dateTime)
{
return AbsoluteStart(dateTime).AddDays(1).AddTicks(-1);
}
This allows you to write:
DateTime.Now.AbsoluteEnd() || DateTime.Now.AbsoluteStart()
or
DateTime partyTime = new DateTime(1999, 12, 31);
Console.WriteLine("Start := " + partyTime.AbsoluteStart().ToString());
Console.WriteLine("End := " + partyTime.AbsoluteEnd().ToString());
I'd use the following:
DateTime now = DateTime.Now;
DateTime startOfDay = now.Date;
DateTime endOfDay = startOfDay.AddDays(1);
and use < endOfDay instead of <= endOfDay. This will mean that it will work regardless of whether the precision is minutes, seconds, milliseconds, ticks, or something else. This will prevent bugs like the one we had on StackOverflow (though the advice was ignored).
Note that it is important to only call DateTime.Now once if you want the start and end of the same day.
try
//midnight this morning
DateTime timeMin = DateTime.Now.Date;
//one tick before midnight tonight
DateTime timeMax = DateTime.Now.Date.AddDays(1).AddTicks(-1)
If you are using this for filtering, as your comments suggest, it is probably a good idea to save DateTime.Now into a variable, just in case the date ticks over between the two calls. Very unlikely but call it enough times and it will inevitably happen one day (night rather).
DateTime currentDateTime = DateTime.Now;
DateTime timeMin = currentDateTime.Date;
DateTime timeMax = currentDateTime.Date.AddDays(1).AddTicks(-1)
One small tweak to hunter's solution above...
I use the following extension method to get the end of the day:
public static DateTime EndOfDay(this DateTime input) {
return input.Date == DateTime.MinValue.Date ? input.Date.AddDays(1).AddTicks(-1) : input.Date.AddTicks(-1).AddDays(1);
}
This should handle cases where the DateTime is either DateTime.MinValue or DateTime.MaxValue. If you call AddDays(1) on DateTime.MaxValue, you will get an exception. Similarly, calling AddTicks(-1) on DateTime.MinValue will also throw an exception.
You must be careful to use
(new DateTime()).AddDays(1).AddTicks(-1);
when it is passed to stored procedure.
It could happen that the value will be approximated to next day.
Like other answerers, I'm not quite sure what you're asking for, but incase you want the smallest possible time and the largest possible time, (not just in a day), then there's DateTime.MinValue and DateTime.MaxValue which return 1/1/0001 12:00:00 AM
and 12/31/9999 11:59:59 PM respectively.
I would advise that you look at this answer:
How can I specify the latest time of day with DateTime
If your original DateTimes also potentially include times, using the AddDays() method will add a full 24 hours, which may not be precisely what you want.
public static DateTime ToEndOfDay(this DateTime time)
{
var endOfDaySpan = TimeSpan.FromDays(1).Subtract(TimeSpan.FromMilliseconds(1));
return time.Date.Add(endOfDaySpan);
}
Please note that if you're passing this time to sql server you should use
dateTime.Date.AddDays(1).AddMilliseconds(-3);
See:
How do I get the last possible time of a particular day
DateTime.Today.AddDays(1).AddSeconds(-1);
Not very exact, but fixed my problems. Now we can use AddMilliseconds, AddTicks and etc. I think it will just vary on what would satisfy your need.

Combine date and time when date is a DateTime and time is a string

I am working with an old mysql database in which a date is stored (without a time) as a datetime and a time is stored as a string (without a date).
In C# I then have a DateTime with a value like 2010-06-25 12:00:00 AM and a String with a value like 15:02.
What is the most concise way to combine these without a lot of overhead?
I have tried a few methods including:
DateTime NewDateTime = DateTime.Parse(OldDateTime.ToString("yyyy-MM-dd ") + TimeString);
I dislike converting the existing DateTime to a string and appending the time.
I can convert the time string to a date, but then I get today's date and adding it as a number of ticks to the old datetime is incorrect.
Note: Don't worry about validation, it is done elsewhere. The time is represented using 24-hour format without seconds.
You can use TimeSpan.Parse to parse the time, and then add the result to the date:
DateTime newDateTime = oldDateTime.Add(TimeSpan.Parse(timeString));
var dt = new DateTime(2010, 06, 26); // time is zero by default
var tm = TimeSpan.Parse("01:16:50");
var fullDt = dt + tm; // 2010-06-26 01:16:50
I used something similar to what simendsjo says, except I continued to have it as a DateTime
DateTime date = Convert.ToDateTime(txtTrainDate.Text);
DateTime time = Convert.ToDateTime(ddTrainTime.SelectedValue);
DateTime dtCOMPLTDTTM = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);
I think you're worrying about the string conversion too much. By combining the 2 string elements together you are saving further date string parsing anyway which will most likely be more expensive.
Is this going to be repeated a lot of times or a simple step in a larger process?
I am fairly sure you could combine and convert these values into a timestamp using SQL.

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.

Categories