0

I have a .netapp application (C#) that I use to extract data from an API.

It is having a problem just now as we are running this for December 2017.. but we want it to name as January 2018. (well 01/01/2018)

I think they way we have written it means it's looking for 13/2017 which obviously doesn't exist.

Can anyone recommend how to ammend this so we can run it just now and how we can ensure we don't run into this issue again next December?

public override string ToString()
    {
        var reportDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 1);

        if (!String.IsNullOrWhiteSpace(AppConfigHelper.EndDate))
        {
            var year = Int32.Parse(AppConfigHelper.EndDate.Substring(6, 4));
            var month = Int32.Parse(AppConfigHelper.EndDate.Substring(3, 2));
            var day = Int32.Parse(AppConfigHelper.EndDate.Substring(0, 2));
            reportDate = new DateTime(year, month, day);
            reportDate = reportDate.AddDays(1);
        }
craig157
  • 355
  • 1
  • 3
  • 18
  • 1
    please share the end date string from the api as well – Barr J Dec 04 '17 at 10:53
  • 1
    As well as the "generating the start of the next month" issue, I'd strongly recommend using `DateTime.ParseExact` to parse the string instead of manually parsing it yourself. – Jon Skeet Dec 04 '17 at 10:58

1 Answers1

3

You can use DateTime.Today.AddMonths(1):

var nextMonth = DateTime.Today.AddMonths(1);
reportDate = new DateTme(nextMonth.Year, nextMonth.Month, 1);
// ...

By the way, you don't need string methods and int.Parse to get the DateTime, use ParseExact:

if (!String.IsNullOrWhiteSpace(AppConfigHelper.EndDate))
{
    reportDate = DateTime.ParseExact(AppConfigHelper.EndDate, "ddMMyyyy", null);
    reportDate = reportDate.AddDays(1);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I was thinking that but sometimes we need to enable the EndDate so we can run for previous months and such. – craig157 Dec 04 '17 at 10:59
  • @craig157: sorry, i dont understand your comment. Ofc you can also use `.AddMonths(-1)` – Tim Schmelter Dec 04 '17 at 11:00
  • Hi Tim - so we might want to gather data from 15.07.2017 so in our App Config we have an EndDate Key that we would set to this date. But if no date set then it runs for Todays Date – craig157 Dec 04 '17 at 11:11
  • @craig157 still i dont understand the Problem. Your question already contains the code to read it. How does this not work anymore with my code? – Tim Schmelter Dec 04 '17 at 11:17
  • 2
    @craig157: You're mentioning things that are all unrelated to each other. The problem stems from a mathematically valid operation (12 + 1 = 13) that causes issues with a date (there is no 13th month). The solution is to **increment the date** rather than incrementing the number of the month. Tim's answer solves that. What you're asking in the comments is a completely different issue that is not related to the problem you asked about. – Flater Dec 04 '17 at 11:33
  • Sorry got it working! There was another Class that wasn't updated! Thank you again for the Solution @TimSchmelter – craig157 Dec 04 '17 at 11:49