I am using DateTime.Today.
Now I'm not sure if the date is from the beginning of the day or the end of the day.
This is what DateTime.Today returns : {11-3-2014 0:00:00}
MSDN states the following: "An object that is set to today's date, with the time component set to 00:00:00."
This means that a DateTime object is created with today's date at the absolute start of the day hence 00:00:00.
You can check if it is the start of the day by using the AddHour() method of the DateTime class.
DateTime d = DateTime.Today;
//AddHours, AddMinutes or AddSeconds
d = d.AddHours(1);
if (d.Date != DateTime.Today.Date)
{
//Not the same day
}
If d.date should be different the date was initialised at a different time (eg. 23:00:01).
http://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx
0:00:00 is the start of the day and 23:59:59 is the end of day.
You can also confirm through this 24-hour clock
In the 24-hour time notation, the day begins at midnight, 00:00, and the last minute of the day begins at 23:59. Where convenient, the
notation 24:00 may also be used to refer to midnight at the end of a
given date[5] – that is, 24:00 of one day is the same time as 00:00 of
the following day.
On a side note:-
If you want to know the time then use .Now because that includes the 10:32:32 or whatever time; however .Today is the date-part only (at 00:00:00 on that day ie the begining of the day). So you can say that .Today is essentially the same as .Now.Date
Thats the beginning of the day, the end of the day would be:
{11-3-2014 23:59:59}
And remember, the only stupid question is the one you don't ask :)
DateTime.Today returns the current DateTime value, without the Time part.
Which means, it is the the first possible DateTime value for the current day.
Think of it like this:
The last moment in a day can be 23:59 or theoretically any amount of nanseconds before the next day. the next day then starts at 00:00:00 counting upwards.
So 11-3-2014 0:00:00 marks the beginning of a day. Either the earliest possible moment, or no time at all, if you want to treat 0:00:00 as a default value.
Related
I have a DateTime property for which I've only set the time:
OpenHour = DateTimeOffset.ParseExact("12:00:00 AM", "hh:mm:ss tt", CultureInfo.InvariantCulture)
But the day has also been saved as the day I've saved this property value.
I only wanted to set the time without the day so that it would be the same time every day.
Is there a way to do this ? Or do I need to create as CronJob to update every date's day to today's day ?
Edit
I'm not trying to create a time only value but know which way is best between creating a Cron Job to update the day of the time every day or if there's a better way.
And I tried to save it as strings but I needed to change it to DateTime to be able to use OrderBy when querying the place's list of hours and I've tried with strings it wasn't working.
Thank you for your help
It is not possible to save "just the time" as a DateTime object.
A DateTime object actually stores the current time as a single integer, and then when you see the result it is filtered through a certain timezone and format.
"The time component of a DateTimeOffset value is measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar. " *(source) https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-5.0
If you want to avoid having to update it every day you could store it as a string representing the time and then have a method to automatically parse that time into todays date into a newly instantiated DateTime object.
string timeOfDay = DateTime.Now.ToString("hh:mm tt");
static DateTime TodayAtTime(string timeOfDay)
{
string calendarDay = DateTime.Now.ToString("MM/dd/yyyy ");
return DateTime.Parse(calendarDay + timeOfDay);
}
DateTime todayAtTime = TodayAtTime(timeOfDay);
Console.WriteLine(todayAtTime.ToString("hh:mm tt MM/dd/yyyy"));
Console.WriteLine(todayAtTime.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
There are 2 events (start and stop of some job) in the past which happened in different time zones.
I need to compute duration of the job.
I can get difference between those time zones in minutes and compute duration like this:
var duration = endTime - startTime.AddMinutes(diff);
However, there is a thing which confuses me.
Suppose start event happend in California and end event was in say Israel.
Right now difference between these time zones is 9 hours - California just switched to the day light and Israel not yet.
Next week this difference will be 10 hours because Israel will switch.
So, duration will be different if computed now and on the next week.
What is the right way to compute it?
Use DateTimeOffset?
So, duration will be different if computed now and on the next week.
I think you're forgetting that its you who is moving through time. not the project start and end times. therefore results will always be the same.
Id be tempted though just to convert the second time into the zone of the first, so it's just a straight diff. to convert from utc to any other zone you can use the following code
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
Theres also good info at https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones
Using c#, I want to compare the current week-to-date to the same period last week-to-date. For example, if today is Wednesday, and if the first day of the week is Sunday, then I want to compare totals for Sunday – Tuesday of this week against Sunday – Tuesday of last week. I’m not counting Wednesday because I don’t have a full day of data until midnight the same day.
The same applies to comparing this mtd to the same number of days last month, and last year. For example, if the current date is June 19th, I want to compare the data from May 1-18th of last month as well as January 1 – June 18th of last year against January 1 – June 18th of this year.
The variables I’m trying to use look like this:
//Current dates
DateTime currentDte = DateTime.Now;
DateTime beginWeek = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
DateTime beginMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime beginYear = new DateTime(DateTime.Now.Year, 1, 1);
//Historical dates
DateTime lastWeekToDate = DateTime.Now.StartOfWeek(DayOfWeek.Sunday - (7 - (int)DateTime.Now.DayOfWeek));
DateTime lastMonthToDate =
DateTime lastYearToDate =
As you can see I figured out the current dates and can loop through them to get the wtd, mtd, and ytd data I need. And I managed to figure out how to get the last week-to-date I need.
But I don’t know how to get the lastMonthToDate and lastYearToDate dates I need. I’ve tried everything I can think of. I’ve read date documentation until my eyes hurt, and still I come up with goose eggs. Can anyone offer any suggestions?
If I understand your question correctly, you just need to use AddMonths and AddYears.
DateTime lastMonthToDate = beginMonth.AddMonths(-1);
DateTime lastYearToDate = beginYear.AddYears(-1);
EDIT
Based your comment - it looks like you want the to subtract months/years from the current date in which case it would be
DateTime lastMonthToDate = currentDte.AddMonths(-1);
DateTime lastYearToDate = currentDte.AddYears(-1);
If it's the time of day that's throwing you off, just use the date part DateTime.Now.Date.AddMonths(-1)
Based on the feedback I received from Zeph, I was able to get the beginning of last year using this code:
DateTime startDate = beginYear.AddYears(-1);
Now I'm able to tally all of the ytd data I need based on the interval dates. Thank you very much!
I have two dates
DateTime date1Z = DateTime.Parse("2014-05-22 23:39:29Z");
DateTime date1ZKind = DateTime.SpecifyKind(DateTime.Parse("2014-05-22 23:39:29Z"), DateTimeKind.Utc);
DateTime date2 = DateTime.Parse("2014-05-22 23:39:29");
DateTime date2Kind = DateTime.SpecifyKind(DateTime.Parse("2014-05-22 23:39:29"), DateTimeKind.Utc);
Console.WriteLine(date1Z);
Console.WriteLine(date1ZKind);
Console.WriteLine(date2);
Console.WriteLine(date2Kind);
Prints
23/05/2014 11:39:29 a.m.
23/05/2014 11:39:29 a.m.
22/05/2014 11:39:29 p.m.
22/05/2014 11:39:29 p.m.
Can someone explain whats going on here?
Using the suffix "Z" is date shorthand for saying that the Date-Time is "Zulu" time which is another word for UTC time. The first two dates are being parsed as UTC, while the last two are being parsed as whatever time is on the computer in question.
So to answer you question of what is going on: the latter two dates are being offset by your local time, which is apparently +12:00 (plus twelve hours), while the first two are not (as they are marked as "Zulu" or UTC time).
You live in New Zealand, which is +12 over UTC. That matches the date difference you are experiencing. As mentioned, the Z stands for UTC.
I'm writing a search program that includes a date range- DateFrom and DateTo, both of which are DateTimes. Searching January - April for any search criteria will return the results of January + February + March + April.
I would like to add functionality whereby a user can choose to search each month within a - range, so searching January - April will return each individual month's results. However I'm having trouble finding an intelligent way to implement this for any unit of time larger than days.
So far I'm getting a TimeSpan using:
TimeSpan ts = query.DateTo - query.DateFrom;
In a perfect world I'd just be able to do something like foreach (month m in TimeSpan){dostuff}, however TimeSpan stores dates as integers and does not include any units larger than days. Additionally, I thought maybe I could just use n = DateFrom.month - DateTo.month to get the difference in months and run a function in a for loop starting with DateFrom and lasting n months, but this won't work between years.
The last case is definitely fixable but includes a number of tedious special cases. Is there a cleaner / more elegant way of accomplishing this sort of iteration that I'm missing?
Thanks.
So for the basic pattern we can use a fairly simple for loop:
public static IEnumerable<DateTime> Months(DateTime start, DateTime end)
{
for (DateTime date = start; date < end; date = date.AddMonths(1))
yield return date;
}
Now in this case we have a start date that is inclusive and an end date that is exclusive. If we want to make the end date inclusive, as you have described, we can add:
end = end.AddMonths(1);
to the start of the method.
Next you have a few other considerations. Are the datetime objects passed in going to always be the first of the month? If not, how do you want to support it? If the start date is Feb 10th do you want the first yielded date to be Feb 1st (the start of the start date's month), March 1st (the first "first day of the month" on or after the start date), or Feb 10th (meaning that each date in the timespan would be the 10th day of that month)?
Those same questions also apply to the end date; do you want the last "first day of the month" before the end date, the first day of the next month, etc.
Also, what should happen if the start date is after the end date? Should it yield the dates "backwards", should it just pretend the start date is the end date and the end date is the start date? Should it keep adding days until you've overflowed DateTime and come back around to that date?
Pretty much all of these issues aren't too hard to deal with, the hard part is just knowing what you want to do in each case.
You could do something like:
var months = ((query.DateTo.Year - query.DateFrom.Year) * 12) + query.DateTo.Month - query.DateFrom.Month
for(int i=0;i<months;i++){
//do stuff as below
var currentDate=query.DateFrom.AddMonths(i);
}