DateTime.Now - first and last minutes of the day - c#

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.

Related

combine parts from 2 different datetime variables into a new datetime variable

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.

How to set timezone based timestamp to get current date and time in C#? [duplicate]

This code has been working for a long time now but has broken now when I try to pass DateTime.Now as the outageEndDate parameter:
public Outage(DateTime outageStartDate, DateTime outageEndDate, Dictionary<string, string> weeklyHours, string province, string localProvince)
{
this.outageStartDate = outageStartDate;
this.outageEndDate = outageEndDate;
this.weeklyHours = weeklyHours;
this.province = province;
localTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[localProvince]);
if (outageStartDate < outageEndDate)
{
TimeZoneInfo remoteTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[province]);
outageStartDate = TimeZoneInfo.ConvertTime(outageStartDate, localTime, remoteTime);
outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime, remoteTime);
The error message I am getting on the last line is that the Kind property is not set correctly on the DateTime parameter (outageEndDate). I've Googled and checked SO for examples but I don't really understand the error message.
Any advice is appreciated.
Regards.
EDIT - The exact error message is:
The conversion could not be completed because the supplied DateTime did not have the Kind
property set correctly. For example, when the Kind property is DateTimeKind.Local, the source
time zone must be TimeZoneInfo.Local. Parameter name: sourceTimeZone
EDIT: outageEndDate.Kind = Utc
Thanks for clarifying your question.
If the DateTime instance Kind is Local, then TimeZoneInfo.ConvertTime will expect the second parameter to be the local timezone of your computer.
If DateTime instance Kind is Utc, then TimeZoneInfo.ConvertTime will expect the second parameter to be the Utc timezone.
You need to convert outageEndDate to the right timezone first, just in case the localProvice timezone doesn't match the timezone on your computer.
outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime);
here is an example of something that you could try
It depends on what you mean by "a GMT + 1 timezone". Do you mean permanently UTC+1, or do you mean UTC+1 or UTC+2 depending on DST?
If you're using .NET 3.5, use TimeZoneInfo to get an appropriate time zone, then use:
// Store this statically somewhere
TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("...");
DateTime utc = DateTime.UtcNow;
DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone );
You'll need to work out the system ID for the Malta time zone, but you can do that easily by running this code locally:
Console.WriteLine(TimeZoneInfo.Local.Id);
If you're not using .NET 3.5, you'll need to work out the daylight savings yourself. To be honest, the easiest way to do that is going to be a simple lookup table. Work out the DST changes for the next few years, then write a simple method to return the offset at a particular UTC time with that list hardcoded. You might just want a sorted List<DateTime> with the known changes in, and alternate between 1 and 2 hours until your date is after the last change:
// Be very careful when building this list, and make sure they're UTC times!
private static readonly IEnumerable<DateTime> DstChanges = ...;
static DateTime ConvertToLocalTime(DateTime utc)
{
int hours = 1; // Or 2, depending on the first entry in your list
foreach (DateTime dstChange in DstChanges)
{
if (utc < dstChange)
{
return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local);
}
hours = 3 - hours; // Alternate between 1 and 2
}
throw new ArgumentOutOfRangeException("I don't have enough DST data!");
}

DateTime returns wrong date

I'm trying to get today's date
DateTime todayDateTime = new DateTime();
and I'm getting this:
{1/1/0001 12:00:00 AM}.
Why is this happening?
Use this
DateTime date = DateTime.Now;
Using new DateTime() creates a DateTime with a time of "0".
If you want todays date you need to use DateTime.Today if you want a DateTime object with a date of today and a time of 12:00:00 AM or DateTime.Now if you want a DateTime with the day and time of the moment you called DateTime.Now.
According to MSDN, the constructor for DateTime which takes in a long initializes by using the specified number of ticks since January 1st, 0001, so saying new DateTime(0) yields this time, not the current time.
Instead, use the static field DateTime.Now to get a DateTime representing the current system time.
In your question you are just initializing the Variable todayDateTime but you have never assigned (set it). This is why it is date ("null")/ beginning of our time calculations.
To actually get todays Date, you can use the following:
DateTime today = DateTime.Today;
first of all you need to assigned a value in the datetime.
just use something like this :
DateTime today = DateTime.Today;

DateTime.MinValue and SqlDateTime overflow

I don't want to validate txtBirthDate so I want to pass DateTime.MinValue in database.
My code:
if (txtBirthDate.Text == string.Empty)
objinfo.BirthDate = DateTime.MinValue;
else
objinfo.BirthDate = DateTime.Parse(txtBirthDate.Text);
DateTime.MinValue return Date = {1/1/0001 12:00:00 AM}
I got a SQL Error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
I under stand it but I don't understand why DateTime.MinValue return invalid date time which is unable to insert in database.How to handle this type of situation?
Very simple avoid using DateTime.MinValue use System.Data.SqlTypes.SqlDateTime.MinValue instead.
Basically, don't use DateTime.MinValue to represent a missing value. You can't use DateTime.MinValue in a SQL Server DateTime field, as SQL Server has a minimum value of the start of 1753.
Instead, make your BirthDate property a Nullable<DateTime> (aka DateTime?), and set it to null when you don't have a value. Also make sure your database field is nullable. Then you just need to make sure that that null ends up as a NULL value in the database. Exactly how you do that will depend on your data access, which you haven't told us anything about.
Well... its quite simple to get a SQL min date
DateTime sqlMinDateAsNetDateTime = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
Although it is an old question, another solution is to use datetime2 for the database column.
MSDN Link
Here is what you can do. Though there are lot many ways to achieve it.
DateTime? d = null;
if (txtBirthDate.Text == string.Empty)
objinfo.BirthDate = d;
else
objinfo.BirthDate = DateTime.Parse(txtBirthDate.Text);
Note: This will work only if your database datetime column is Allow Null. Else you can define a standard minimum value for DateTime d.
I am using this function to tryparse
public static bool TryParseSqlDateTime(string someval, DateTimeFormatInfo dateTimeFormats, out DateTime tryDate)
{
bool valid = false;
tryDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
System.Data.SqlTypes.SqlDateTime sdt;
if (DateTime.TryParse(someval, dateTimeFormats, DateTimeStyles.None, out tryDate))
{
try
{
sdt = new System.Data.SqlTypes.SqlDateTime(tryDate);
valid = true;
}
catch (System.Data.SqlTypes.SqlTypeException ex)
{
}
}
return valid;
}
From MSDN:
Date and time data from January 1, 1753, to December 31, 9999, with an
accuracy of one three-hundredth second, or 3.33 milliseconds. Values
are rounded to increments of .000, .003, or .007 milliseconds. Stored
as two 4-byte integers. The first 4 bytes store the number of days
before or after the base date, January 1, 1900. The base date is the
system's reference date. Values for datetime earlier than January 1,
1753, are not permitted. The other 4 bytes store the time of day
represented as the number of milliseconds after midnight. Seconds have
a valid range of 0–59.
SQL uses a different system than C# for DateTime values.
You can use your MinValue as a sentinel value - and if it is MinValue - pass null into your object (and store the date as nullable in the DB).
if(date == dateTime.Minvalue)
objinfo.BirthDate = null;
Simply put, don't use DateTime.MinVaue as a default value.
There are a couple of different MinValues out there, depending which environment you are in.
I once had a project, where I was implementing a Windows CE project, I was using the Framework's DateTime.MinValue (year 0001), the database MinValue (1753) and a UI control DateTimePicker (i think it was 1970). So there were at least 3 different MinValues that were leading to strange behavior and unexpected results. (And I believe that there was even a fourth (!) version, I just do not recall where it came from.).
Use a nullable database field and change your value into a Nullable<DateTime> instead. Where there is no valid value in your code, there should not be a value in the database as well. :-)
If you use DATETIME2 you may find you have to pass the parameter in specifically as DATETIME2, otherwise it may helpfully convert it to DATETIME and have the same issue.
command.Parameters.Add("#FirstRegistration",SqlDbType.DateTime2).Value = installation.FirstRegistration;
use extensions
public static class DateTimeExtensions
{
public static DateTime MinValue(this DateTime sqlDateTime)
{
return new DateTime(1900, 01, 01, 00, 00, 00);
}
}
DateTime date = DateTime.Now;
Console.WriteLine("Minvalue is {0} ", date.MinValue().ToShortDateString());

Convert Timeformat to normal datetime

I have to convert this time format string (20130221191038.576375+330) to normal datetime through c# classes like datetime.
please share the idea to convert this..
Thanks guys, I got the solution for my requirement with the help of Erik,tung and BlackHatShadow.
Referred this
this also
The format you have is a CIM_DATETIME value, which is almost simple to parse. The only problem is that it specifies the timezone as an offset in number of minutes.
You can use the DateTime.TryParseExact to convert the portion of the string prior to the timezone specifier, then subtract the timezone value (in minutes) from the result to get the UTC datetime. Then you can convert to local time if you need, or leave it in UTC form.
public static DateTime? CIMToUTCDateTime(string CIM_DATETIME)
{
// CIM_DATETIME must be 25 characters in length
if (string.IsNullOrEmpty(CIM_DATETIME) || CIM_DATETIME.Length != 25)
return null;
// Get the datetime portion of the string without timezone offset
string dtPortion = CIM_DATETIME.Substring(0, 21);
// convert to datetime
DateTime dt;
if (!DateTime.TryParseExact(dtPortion, "yyyyMMddHHmmss.ffffff", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeUniversal | System.Globalization.DateTimeStyles.AdjustToUniversal, out dt))
return null;
// subtract timezone offset to get UTC time equivalent
int tzoffset;
if (!Int32.TryParse(CIM_DATETIME.Substring(21), out tzoffset))
return null;
dt = dt.AddMinutes(-tzoffset);
// return UTC datetime
return dt;
}
And now that I've written this horrid little method, you've gone and found another solution. Typical :P
I know you already found a solution, but I came across this nice ManagementDateTimeConverter .Net class that does exactly what you want. All you need to do is:
// This gets converted to your local time
DateTime converted = ManagementDateTimeConverter.ToDateTime("20130221191038.576375+330")
// If you want the UTC equivalent:
converted.ToUniversalTime()

Categories