How to get the start date and end date of month in different variable. I have tried this and I get the start date but unable to find the end date
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");
This logic fails when month end date is 31 and 28 or 29. Your Help are surely appretiated.
You can calculate endDate like this:
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
To get First Date
public DateTime FirstDayOfMonth(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
To get Last Date
public DateTime LastDayOfMonth(DateTime dateTime)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
.AddMonths(1).AddDays(-1);
You already had the start date :
DateTime monthStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
There's a method to get the number of days in a month (and Looking at the IL code, it seems that this way is more efficient than the other answers, though unless you're going to do it a billion time, I doubt there will be any difference) :
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime monthEndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, daysInMonth);
For first date:
DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
For last date:
DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));
Related
I have a start date and end date during a week, and I have to subtract each day that passes during the week. As each day goes by I want to display 6 days left, 5 days left, etc.
I tried this:
DateTime endDate = DateTime.Now.AddDays(-1);
if (endDate == startDate)
{
// do something
}
But it's not working.
I believe the user is asking for time left between now and a specific date.
DateTime dateTime = new DateTime(2019, 12, 25);
TimeSpan timeLeft = dateTime - DateTime.Now; // At time of posting, it is 18/12/2019
Console.WriteLine($"{timeLeft.Days} days left");
Because you are always subtracting from DateTime.Now (DateTime endDate = DateTime.Now.AddDays(-1);)
DateTime endDate = DateTime.Today;
DateTime startDate = DateTime.Today.AddDays(-7);
do
{
enddate = enddate.AddDays(-1);
}while(enddate>=startdate);
how can I calculate if a date (in DateTime format) is 6 month later or not from my BirthDate (in DateTime format)?
Use DateTime AddMonth method
https://msdn.microsoft.com/ru-ru/library/system.datetime.addmonths(v=vs.110).aspx
var dat = new DateTime(2015, 12, 31);
var dat2 = new DateTime(2015, 12, 31);
if (dat.AddMonths(6) < dat2) { ... }
You should use DateTime.AddMonths :
DateTime dt;
DateTime birthDate;
if (dt <= birthDate.AddMonths(6))
{
}
enter your birth date, calculate your next birthday and compare the dates,
var born = new DateTime(1900, 02, 01);
var checkdate = DateTime.Now;
var nextBirthday = new DateTime(DateTime.Now.Year, born.Month, born.Day);
if (nextBirthday < DateTime.Now)
{
nextBirthday = new DateTime(DateTime.Now.Year + 1, born.Month, born.Day);
}
if (checkdate.AddMonths(6) < nextBirthday)
{
Console.WriteLine("date is 6 months later then birthday");
}
else
{
Console.WriteLine("wait for it");
}
DateTime birthDate=new DateTime(year,month,day);
DateTime dateToCompare = new DateTime(year, month, day);
if(dateToCompare >= birthdate.AddMonths(6))
{
//DoSomething
}
You could calculte the difference between dates using Subtract method and calculate how many months you have between these dates, for sample:
DateTime birthDay = /* some date */;
DateTime someDate = /* some date */;
var months = someDate.Subtract(birthDay).Days / (365.25 / 12);
This answer provides a good helper for Dates:
https://stackoverflow.com/a/33287670/316799
I am looking for a elegant solution (if it exists) to achieve what I described in the title.
I saw an elegant solution for "changing the time in a DateTime object" and it is as follows:
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
If there such a solution for changing the date in a DateTime object?
DateTime struct is designed to be immutable, so you can't change it. You can get a new one based on values from the old one, which is exactly what your solution does.
You can make it a bit more clear by using DateTime constructor which takes all date and time values: year, month, day, hour, minute and seconds.
s = new DateTime(s.Year, s.Month, s.Day, 10, 30, 0);
s = new DateTime(2014, 10, 2, s.Hour, s.Minute, s.Second);
or you can use TimeOfDay property:
s = new DateTime(2014, 10, 2) + s.TimeOfDay;
Assuming that you are going to pass a new DateTime object to update your existing object, a function like this can work.
DateTime UpdateDate(DateTime existingDate, DateTime newDate)
{
return newDate.Date + existingDate.TimeOfDay;
}
myDatetime = new DateTime(year, month, day);
You can use the DateTime constructors, so you can preserve the original time.
See this example:
var firstDate = new DateTime(2015, 01, 01, 15, 0, 0);
var newDate = DateTime.Now;
// New date, original time.
firstDate = new DateTime(newDate.Year, newDate.Month, newDate.Day, firstDate.Hour, firstDate.Minute, firstDate.Second, firstDate.Millisecond);
Try this:
DateTime s = ...;
s = new DateTime(year, month, day) + s.TimeOfDay;
That preserves the time in the original DateTime.
Try this:
DateTime now = DateTime.Now;
DateTime dateOnly = now.Date;
TimeSpan time = now.TimeOfDay;
dateOnly = DateTime.Parse("5/15/15"); // or whatever date you choose
DateTime newDateTime = dateOnly + time;
Assuming I can not change service that returns data, I am left with
var date = "20140231";
var scope = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture);
Clearly "20140231" is lazy way of saying end of February. What is the cleanest way to get last date of February with input of "20140231"?
There is 1 constraint - this should work with .net 2.0.
string date = "20140231";
DateTime result;
int year = Convert.ToInt32(date.Substring(0, 4));
int month = Convert.ToInt32(date.Substring(4, 2));
int day = Convert.ToInt32(date.Substring(6, 2));
result = new DateTime(year, month, Math.Min(DateTime.DaysInMonth(year, month), day));
February can have only 28 or 29 days depends on current year is a leap year or not.
It can't have 30 or 31 days in any year. That's why you can't parse your 20140231 string successfully.
You can clearly get the last day of February like;
DateTime lastDayOfFebruary = (new DateTime(2014, 2, 1)).AddMonths(1).AddDays(-1);
If your service always get year as a first 4 character, you can use .Substring() to get year and pass DateTime constructor as a year.
var date = "20140231";
string year = date.Substring(0, 4);
DateTime lastDayOfFebruary = (new DateTime(int.Parse(year), 2, 1)).AddMonths(1).AddDays(-1);
You could create a while, cut the date in pieces, and keep subtracting one from the day part until it is a valid date. This should really be fixed on the entry side though.
Try this:
var date = "20140231";
DateTime scope;
bool dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
while (!dateValid)
{
string yearMonth = date.Substring(0, 4);
int day = Convert.ToInt32(date.Substring(6, 2));
if (day > 1)
{
day--;
}
else
{
break;
}
date = yearMonth + day.ToString().PadLeft(2, '0');
dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
}
I have here 2 datepicker for start date and end date.
how can I get the first day and last day of the current month
rdpStartDate.SelectedDate = DateTime.Now;
rdpEndDate.SelectedDate = DateTime.Now;
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
var now = DateTime.Now;
var first = new DateTime(now.Year, now.Month, 1);
var last = first.AddMonths(1).AddDays(-1);
You could also use DateTime.DaysInMonth method:
var last = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
var myDate = DateTime.Now;
var startOfMonth = new DateTime(myDate.Year, myDate.Month, 1);
var endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
That should give you what you need.
Try this code it is already built in c#
int lastDay = DateTime.DaysInMonth (2014, 2);
and the first day is always 1.
Good Luck!
An alternative way is to use DateTime.DaysInMonth to get the number of days in the current month as suggested by #Jade
Since we know the first day of the month will always 1 we can use it as default for the first day with the current Month & year as current.year,current.Month,1.
var now = DateTime.Now; // get the current DateTime
//Get the number of days in the current month
int daysInMonth = DateTime.DaysInMonth (now.Year, now.Month);
//First day of the month is always 1
var firstDay = new DateTime(now.Year,now.Month,1);
//Last day will be similar to the number of days calculated above
var lastDay = new DateTime(now.Year,now.Month,daysInMonth);
//So
rdpStartDate.SelectedDate = firstDay;
rdpEndDate.SelectedDate = lastDay;
string firstdayofyear = new DateTime(DateTime.Now.Year, 1, 1).ToString("MM-dd-yyyy");
string lastdayofyear = new DateTime(DateTime.Now.Year, 12, 31).ToString("MM-dd-yyyy");
string firstdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("MM-dd-yyyy");
string lastdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("MM-dd-yyyy");