Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to display the week number dynamically in a drop down after selecting the "fromdate" and "todate" from the calendar popup
I am trying to auto fill the drop down "ddlWeekNo" from fromDate and toDate

Please assist
Thanks in advance

What I have tried:

DateTime fromDate = Convert.ToDateTime(txtFrom.Text);
           DateTime toDate = Convert.ToDateTime(txtTo.Text);
           String WeekFromTo = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(fromDate.Month).ToString() + " " + fromDate.Day.ToString() + " " + fromDate.Year.ToString() + " to " + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(toDate.Month).ToString() + " " + toDate.Day.ToString() + " " + toDate.Year.ToString();
           String WeekString = "Week " + ddlWeekNo.SelectedValue + ": " + WeekFromTo;
Posted
Updated 28-Jun-18 23:33pm
v3
Comments
Karthik_Mahalingam 27-Sep-17 23:31pm    
what is the issue ?
Richard Deeming 28-Sep-17 12:28pm    
String WeekFromTo = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(fromDate.Month).ToString() + " " + fromDate.Day.ToString() + " " + fromDate.Year.ToString() + " to " + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(toDate.Month).ToString() + " " + toDate.Day.ToString() + " " + toDate.Year.ToString();


That can be simplified to:
string WeekFromTo = string.Format(CultureInfo.CurrentCulture, "{0:MMMM d yyyy} to {1:MMMM d yyyy}", fromDate, toDate);


Or, if you're using VS2017 / C# 6:
string WeekFromTo = $"{fromDate:MMMM d yyyy} to {toDate:MMMM d yyyy}";


Custom Date and Time Format Strings | Microsoft Docs[^]

 
Share this answer
 
When displaying year-weeknr in TextBox with TextMode=Week
public class DateHelpers
    {
        /// <summary>
        /// Gets the current week number.
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentWeekNumber()
        {
            var weeknum = Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return DateTime.Now.ToString($"{DateTime.Now.Year}-W{weeknum}");
        }

        /// <summary>
        /// Gets the current week number for a specified DateTime object.
        /// </summary>
        /// <param name="dateTime">The date time.</param>
        /// <returns></returns>
        public static string GetCurrentWeekNumber(DateTime dateTime)
        {
            var weeknum = Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return dateTime.ToString($"{dateTime.Year}-W{weeknum}");
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900