Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can i get year, month different from calendar?

How can i get year, month different from calendar?

Scheduled Pinned Locked Moved C#
questiontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    buffering83
    wrote on last edited by
    #1

    If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.

    hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~

    P A 2 Replies Last reply
    0
    • B buffering83

      If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.

      hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      How long is a year? As you found, TimeSpan won't/can't do that, but you can probably do it manually.

      1 Reply Last reply
      0
      • B buffering83

        If user click dates such as 2008-06-12 ~ 2011-09-25 I want to get year, month different not days. example) 3years 2month I am using TimeSpan function. but this function only show day's difference.

        hi My english is a little. anyway, nice to meet you~~ and give me your advice anytime~

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        It depends on what you really want. For example 3 Years and 2 months could be between 1154 and 1158 days depending on which months you're actually talking about and if a leap year was included. But I think for what you want you're going to have to just check the difference between the years/months/days yourself, I reckon something like this:

        public struct Diff
        {
        public int Years;
        public int Months;
        public int Days;
        };

        //Will return incorrect negative values if end < start
        public Diff Difference(DateTime start, DateTime end)
        {
        Diff diff = new Diff();

        //Not as far into month, so it doesn't count
        if (end.Day < start.Day)
        {
        end = end.AddMonths(-1);

        //Calc diff
        int remainDays = DateTime.DaysInMonth(start.Year, start.Month) - start.Day;
        diff.Days = remainDays + end.Day;
        

        }
        else
        diff.Days = end.Day - start.Day;

        //Not as far into year, so it doesn't count
        if (end.Month < start.Month)
        {
        end = end.AddYears(-1);

        //Calc diff
        diff.Months = 12 - (start.Month - end.Month);
        

        }
        else
        diff.Months = end.Month - start.Month;

        diff.Years = end.Year - start.Year;

        return diff;
        }

        Which puts your example at 3 years and 3 months (and 13 days) since your further into September than you are into June, if you used 2008-06-12 ~ 2011-09-8 it would give 3 years 2 months 26 days. Which may or may not be quite what you want, but I think you get the idea. Edit: Fixed function, mustn't write code when sleepy

        -SK Genius

        Vehicle Simulation Demo - New and Improved!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups