0

for example i have the start date = 05-09-2016(dd-mm-yyyy) and end date = 09-01-2017

and i want to have the week starting from 5th sep to be called week 1 and the last week to be 18

EDIT: i shall be giving a date and from these two start and end dates it should give me the week number. for eg. when i enter 09-11-2016 it should give me week 6

and i have to acheive this without doing any hardcoding.

uptil now i have written this

DateTime date = new DateTime(2016, 09, 05   );
        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        Calendar cal = dfi.Calendar;

        Console.WriteLine( (cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek))-36);

but this doesn't work when the new year starts .. any advice please? also tell if we can acheive something similar using javascript if not acheivable in asp.net/ c#

1
  • If the start is 5 September, how can 11 September be 6 weeks later? Commented Nov 13, 2016 at 22:31

2 Answers 2

1

It looks you need to calculate the number of weeks between two dates, please give a try as below:

This prints 18 as you expected.

    DateTime date1 = new DateTime(2016, 09, 05);
    DateTime date2 = new DateTime(2017, 01, 09);
    var weeks = Math.Ceil((date2 - date1).TotalDays / 7);

    Console.WriteLine(weeks);
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I can't quite understand what you are after. Do you want week days or week number? If week number then between two dates or from the start of the month or year. Please precise more. I am now confused with your updated example above "for eg. when i enter 11-09-2016 it should give me week 6". What is this 6 means?
Yes then, that's what my code is doing. If you execute, it will give the same. Even you can add "Math.Ceil" if required. Also, this will work with the new year as you said. Can you please advise what's wrong with the code by executing it with multiple date?
You expect calculate the x weeks from the year start for the first date then y for the second date and subtracting them which will break when new year comes. Instead if you subtract both the dates and calculate weeks between them, it will give you exactly what you need. You can even change this line to "var weeks = Math.Ceil((date2 - date1).TotalDays / 7);"
Does DateTime(2016, 09, 05) create a date for 5 September or 5 October?
@RobG 5th September
0

i wrote you an example of how to do it with an iterator and summarize data you have on a list with dates...

            public class CurrentWeek {
                public int Count{get;set;}
                public DateTime Date{get;set;}
            }

            var totalDays = (int)(toDate - fromDate).TotalDays;
            while ((int)(toDate - fromDate).TotalDays > 0)
            {
                CurrentWeek currentWeek = new CurrentWeek ();
                currentWeek.Date = fromDate;
                // here i collect the data from my list of dates
                var rslt = listOpen.Where(x => x.Date >= fromDate && x.Date <= fromDate.AddDays(7)).Select(y => y.Count).Sum();
                currentWeek.Count = rslt;

                rtrn.Add(currentWeek);
                fromDate = fromDate.AddDays(7);
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.