How can I set certain DateTime value to tomorrow 9:00 AM
For example:
DateTime startTime = new DateTime.Now.AddDays(1).//setTime to 9:00 AM
Is there some SetDateTime value functionality that I don't know?
You can use two methods
DateTime.Today.AddDays(1).AddHours(9)
You can use this DateTime constructor like;
DateTime tomorrow = new DateTime(DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day + 1,
9,
0,
0);
Console.WriteLine(tomorrow);
Output will be;
18.03.2014 09:00:00
As CompuChip mentioned, this throws exception if the current day is the last day of the month.
Better you can use DateTime.Today property with AddDays(1) and AddHours(9) because it get's to midnight of the current day. Like;
DateTime tomorrow = DateTime.Today.AddDays(1).AddHours(9);
DateTime dt=DateTime.Now;
DateTime Tomorrow = new DateTime(dt.Year,dt.Month,dt.Day+1,9,0,0);
Related
I have to calculate the date or create a date which is 10 days into next month. I have with me month and year. If the month is April and year is 2019 I need a date which is 10th of may 2019.
You can simply use the methods available in the DateTime structure to do math with dates
// As example, replaces it with whatever date you have
DateTime current = new DateTime(2019,4,5);
DateTime next = new DateTime(current.Year, current.Month, 1).AddMonths(1).AddDays(9);
You can use AddMonths() and AddDays() extension method of DateTime.
From MSDN :
AddMonths() : Returns a new DateTime that adds the specified number of
months to the value of this instance.
AddDays() : Returns a new DateTime that adds the specified number of
days to the value of this instance.
//Considered this is your Current date
DateTime existingDate = new DateTime(2019, 4, 1);
//Below code will add +1 month to current month and +9 days to current date.
var result = existingDate.AddMonths(1).AddDays(9);
Output :
CurrentDate : 4/1/2019 12:00:00 AM
Next Date (+1 month and +9 days) :5/10/2019 12:00:00 AM
POC : .net Fiddle
var dateNow = DateTime.Now.AddMonths(1);
var date = new DateTime(dateNow.Year, dateNow.Month, 10);
Try this:
DateTime dt = new DateTime(2019, 04, 01);
DateTime newDT = dt.AddMonths(1).AddDays(9);
I have a date like "11/01/2015" and I want to know the first day for month 11?
DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");
I thought about that but I don't know how to retrieve the day in French!!
thanks
You can use the DayOfWeek property on a DateTime instance constructed with the year/month of the input date:
var time = DateTime.Parse("11/01/2015");
var firstDayOfMonth = new DateTime(time.Year, time.Month, 1);
Console.WriteLine(firstDayOfMonth.DayOfWeek);
To get the day in French use the CultureInfo of the language you are interested in :
var culture = new System.Globalization.CultureInfo("fr-FR");
Console.WriteLine(culture.DateTimeFormat.GetDayName(firstDayOfMonth.DayOfWeek));
Output:
Sunday
dimanche
I have a code:
int MonthNow = System.DateTime.Now.Month;
int YearNow = System.DateTime.Now.Year;
int DayNow = System.DateTime.Now.Day;
How can I get yesterday and tomorrow day, month and year in C#?
Of course, I can just write:
DayTommorow = DayNow +1;
but it may happen that tomorrow is other month or year. Are there in C# built-in tools to find out yesterday and today?
DateTime tomorrow = DateTime.Today.AddDays(1);
DateTime yesterday = DateTime.Today.AddDays(-1);
You can find this info right in the API reference.
var today = DateTime.Today;
var tomorrow = today.AddDays(1);
var yesterday = today.AddDays(-1);
You should do it this way, if you want to get yesterday and tomorrow at 00:00:00 time:
DateTime yesterday = DateTime.Today.AddDays(-1);
DateTime tomorrow = DateTime.Today.AddDays(1); // Output example: 6. 02. 2016 00:00:00
Just bare in mind that if you do it this way:
DateTime yesterday = DateTime.Now.AddDays(-1);
DateTime tomorrow = DateTime.Now.AddDays(1); // Output example: 6. 02. 2016 18:09:23
then you will get the current time minus one day, and not yesterday at 00:00:00 time.
Today :
DateTime.Today
Tomorrow :
DateTime.Today.AddDays(1)
Yesterday :
DateTime.Today.AddDays(-1)
You want DateTime.Today.AddDays(1).
Use DateTime.AddDays() (MSDN Documentation DateTime.AddDays Method).
DateTime tomorrow = DateTime.Now.AddDays(1);
DateTime yesterday = DateTime.Now.AddDays(-1);
The trick is to use "DateTime" to manipulate dates; only use integers and strings when you need a "final result" from the date.
For example (pseudo code):
Get "DateTime tomorrow = Now + 1"
Determine date, day of week, day of month - whatever you want - of the resulting date.
To get "local" yesterday in UTC.
var now = DateTime.Now;
var yesterday = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Utc).AddDays(-1);
Beware of adding an unwanted timezone to your results, especially if the date is going to be sent out via a Web API. Use UtcNow instead, to make it timezone-less.
Hi all i want a datetime object. In which I want to set time part by my own.
Like i want DateTime object who will have todays system date and I want to set its time to 08:00:00.
Here you are:
DateTime structure
var myDate = DateTime.Today.AddHours(18);
Will give you a date value of The current date, at 6pm.
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0);
That is the simplest I guess, in another case you can also create an extension method on DateTime probably to return you the above!
An extension method will also enable you to call it something like DateTime.MyToday() which will return you the current date with time set to 8:00:00 by default. So you need not do the entire new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0); every time you need this kind of date.
DateTime meetingAppt = new DateTime(
DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:
dateTimeWycenaPortfelaObliczDataOd.Date
to get the date part only (time will be 00:00:00...).
If you want to get the very last tick of the date, you can use:
dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)
but you really better work with the next date (.AddDays(1)).
In any case, there is no need to convert to string and back to DateTime.
DateTime objects have a Date property which might be what you need.
You can use the following properties / methods on a DateTime object to get your values :
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
It would help to know why you're needing it, but this would work.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);
Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.
You could use the Date property of the DateTime object to accomplish what you need.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);
If you really want it to end at 23:59:59 you can do:
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);
Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.
Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);
DateTime varObliczDo = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
You could work with TimeSpan:
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);
Like that you avoid at least the parsing, which can fail depending on the local culture settings.