Suppose my input is March 31, 2015.
How would I be able to get the days of the week that March 31, 2015 is in?
In this case, it should output:
March 29, 2015-April 4, 2015
I found something similar here but it's not quite what I'm looking for as that one takes in the week number while my input is a date itself.
DateTime date = new DateTime(2015, 3, 31);
DateTime weekFirstDay = date.AddDays(DayOfWeek.Sunday - date.DayOfWeek);
DateTime weekLastDay = weekFirstDay.AddDays(6);
Here is a way to get each value separately from a given DateTime
Week start:
public static DateTime GetWeekStartDate(DateTime value)
{
return value.AddDays(-(int)value.DayOfWeek).Date;
}
Week end:
public static DateTime GetWeekEndDate(DateTime value)
{
return value.AddDays(6 - (int)value.DayOfWeek).Date;
}
DayOfWeek firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
DateTime startDate = DateTime.Now;
while (firstDayOfWeek != startDate.DayOfWeek)
{
startDate = startDate.AddDays(-1);
}
DateTime firstDay = startDate.Date;
DateTime lastDay = startDate.AddDays(6);
Related
I need help in C# getting month names from current month, meaning user inputs a month(name) and will return the list of months from the starting month until the current month.
Example; user inputs "August" and current month is "December" so it should return "August, September, October, November, December".
I've done a few steps but still can't get to it.
1st try:
string pattern = ("MMMM/yyyy");
Console.WriteLine("Enter Month: MMMM/yyyy");
DateTime inpMonth = DateTime.ParseExact(Console.ReadLine(),pattern,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
string last = inpMonth.ToString("MMMM/yyyy");
DateTime date = DateTime.Now;
string curr = date.ToString("MMMM/yyyy");
2nd Try(new step):
//First Date
DateTime 1Date = new DateTime(2020, 12, 01);
//Second Date
DateTime 2Date =new DateTime(2019, 01, 01);
int month1 = (2Date.Month - 1Date.Month);
int month2 = (2Date.Year - 1Date.Year) * 12;
int months = month1 + month2;
string mon = months.DateTime.ToString("MMMM"); //trying to convert the month number to month name
Both try seems to not get any close result..
The following code will output to the result you requested:
var start = "August";
var today = DateTime.Today;
var date = new DateTime(today.Year, DateTime.ParseExact(start, "MMMM", CultureInfo.CurrentCulture).Month, 1);
while (date < today)
{
Console.WriteLine($"{date:MMMM}");
date = date.AddMonths(1);
}
There will be 2 buttons of toggle.One will be up and other will be down
Query is if current month is of 30 days then it should display 01 Aug 2017 to 16th Aug 2017 when i toggle up it should show 16th Aug to 30th Aug and similar should work for toggle down.
But if current month is of 31 days then it should display 01 Aug 2017 to 16th Aug 2017 and 16th Aug to 31st Aug and similar should work for toggle down.This should work continous if am trying to toggle up it should display like below
1st Jan to 15th jan then 16th to 30th Jan then 1st Feb to 16th Feb like wise it should go on for toggle up and down.
Code which i tried is below :
public static DateTime GetEndDate(int year, int month)
{
decimal currentmonth = System.DateTime.DaysInMonth(year, month);
decimal value = Math.Ceiling(Convert.ToDecimal(currentmonth / 2));
// DateTime updatedfinaldatevaluestart = new DateTime(year, month, day);
DateTime updatedfinaldatevalueend = new DateTime(year, month, Convert.ToInt32(value));
return updatedfinaldatevalueend;
}
public static DateTime GetStartDate(int year, int month)
{
decimal currentmonth = System.DateTime.DaysInMonth(year, month);
decimal value = Math.Ceiling(Convert.ToDecimal(currentmonth / 2));
// DateTime updatedfinaldatevaluestart = new DateTime(year, month, day);
DateTime updatedfinaldatevalueend = new DateTime(year, month, 01);
return updatedfinaldatevalueend;
}
You don't have to do all these calculations. The first half for all months will always be between the 1st and the 15th (you don't have to calculate 15th. There's no month that has like 45 days). And, the second half will always start from the 16th, and you can get the end date of the month by doing this (startDate is the first day of the month):
var endDate = startDate.AddMonths(1).AddDays(-1);
I suspect there is a better way of doing what you're trying to achieve but there isn't enough information in the question to determine what that is (what is calling these methods, what is happening to the output etc) However, using the method you have posted, this (untested) code may do what you require
The bool parameter should be true when the down button is clicked and false when the up button is clicked
public static DateTime GetEndDate(int year, int month, bool firstHalfOfMonth)
{
if (firstHalfOfMonth == false)
{
return GetStartDate(year, month, false);
}
else
{
return new DateTime(year, month, System.DateTime.DaysInMonth(year, month));
}
}
public static DateTime GetStartDate(int year, int month, bool firstHalfOfMonth)
{
if (firstHalfOfMonth == false)
{
int daysInCurrentMonth = System.DateTime.DaysInMonth(year, month);
int midMonth = Convert.ToInt32(Math.Ceiling((double)daysInCurrentMonth / 2));
return new DateTime(year, month, midMonth);
}
else
{
return new DateTime(year, month, 1);
}
}
I need to calculate holidays which always occur on varying day in a certain week every year. They usually occur on a first weekday in a specific week and month.
I have these properties: Name, Month, DayOfWeek, WeekOfMonth. How do I use DateTime objects to calculate the Day that the holiday falls on?
Notice in the code below how they handle Labor Day and Thanksgiving. You'll probably have to do something like this for other holidays that don't always fall on a specific date.
I got this code from U.S. Holiday List in C#
private static HashSet<DateTime> GetHolidays(int year)
{
HashSet<DateTime> holidays = new HashSet<DateTime>();
//NEW YEARS
DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1).Date);
holidays.Add(newYearsDate);
//MEMORIAL DAY -- last monday in May
DateTime memorialDay = new DateTime(year, 5, 31);
DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
memorialDay = memorialDay.AddDays(-1);
dayOfWeek = memorialDay.DayOfWeek;
}
holidays.Add(memorialDay.Date);
//INDEPENCENCE DAY
DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4).Date);
holidays.Add(independenceDay);
//LABOR DAY -- 1st Monday in September
DateTime laborDay = new DateTime(year, 9, 1);
dayOfWeek = laborDay.DayOfWeek;
while(dayOfWeek != DayOfWeek.Monday)
{
laborDay = laborDay.AddDays(1);
dayOfWeek = laborDay.DayOfWeek;
}
holidays.Add(laborDay.Date);
//THANKSGIVING DAY - 4th Thursday in November
var thanksgiving = (from day in Enumerable.Range(1, 30)
where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).ElementAt(3);
DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving);
holidays.Add(thanksgivingDay.Date);
DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25).Date);
holidays.Add(christmasDay);
return holidays;
}
public static DateTime AdjustForWeekendHoliday(DateTime holiday)
{
if (holiday.DayOfWeek == DayOfWeek.Saturday)
{
return holiday.AddDays(-1);
}
else if (holiday.DayOfWeek == DayOfWeek.Sunday)
{
return holiday.AddDays(1);
}
else
{
return holiday;
}
}
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 a financial year's month end value 2.
How would i calculate the financial year DateTime startDate and DateTime endDate from that value?
You can do:
DateTime startDate = new DateTime(DateTime.Today.Year, 2, 1); // 1st Feb this year
DateTime endDate = new DateTime(DateTime.Today.Year+1, 2, 1).AddDays(-1); // Last day in January next year
Does that solve your problem?
I assume you mean Feb by 2.
This code should do this:
DateTime start = new DateTime(2010,2,1);
DateTime end = start.AddMonths(12).AddDays(-1);
Console.WriteLine(start);
Console.WriteLine(end);
Output:
01-Feb-10 12:00:00 AM
31-Jan-11 12:00:00 AM
Here is my version for calculating the Fiscal Year Start Date. It checks the StartMonth against the current month, and will adjust the year.
private DateTime? FiscalYearStartDate() {
int fyStartMonth = 2;
var dte = new DateTime(DateTime.Today.Year, fyStartMonth, 1); // 1st April this year
if (DateTime.Today.Month >= fyStartMonth) {
//Do nothing, since this is the correct calendar year for this Fiscal Year
} else {
//The FY start last calendar year, so subtract a year
dte = dte.AddYears(-1);
}
return dte;
}
You can easily calculate the End Date like others have done, by adding +1 Year, and then subtracting 1 Day (thanks to Johannes Rudolph).
DateTime endDate = new DateTime(DateTime.Today.Year+1, 2, 1).AddDays(-1);
If your current date is 14/01/2021
Then the Indian financial Year is 01/04/2020 to 31/03/2021
Use the following code for perfect output.
DateTime CurrentDate = DateTime.Now;
int CurrentMonth = CurrentDate.Month;
if (CurrentMonth >= 4)//4 is the first month of the financial year.
{
txtFromDate.Text = new DateTime(CurrentDate.Year, 4, 1).ToString(CS.ddMMyyyy);
txtToDate.Text = new DateTime(CurrentDate.Year + 1, 4, 1).AddDays(-1).ToString(CS.ddMMyyyy);
}
else
{
txtFromDate.Text = new DateTime(CurrentDate.Year - 1, 4, 1).ToString(CS.ddMMyyyy);
txtToDate.Text = new DateTime(CurrentDate.Year, 4, 1).AddDays(-1).ToString(CS.ddMMyyyy);
}
public static (DateTime, DateTime) GetCurrentFinacialYearDateRange()
{
if(DateTime.Now.Month >= 7)
{
DateTime startDate = new DateTime(DateTime.Today.Year, 7, 1); // 1st July this year
DateTime endDate = new DateTime(DateTime.Today.Year + 1, 7, 1).AddDays(-1); // Last day in June next year
return (startDate, endDate);
}
else
{
DateTime startDate = new DateTime(DateTime.Today.Year-1, 7, 1); // 1st July this year
DateTime endDate = new DateTime(DateTime.Today.Year, 7, 1).AddDays(-1); // Last day in June next year
return (startDate, endDate);
}
}