Day of the week to get the date

Source: Internet
Author: User

I'm a little bit trickery this method, only after 1900-01-01 of data, before the date needs to be recalculated.

1. First remove the number of days from the date of 1900-01-01

        private static int GetDays (int year, int month, Int. day) {//1900-1-1 is Monday int result            = 0;            Int[] month31 = {1,3,5,7,8,10,12};            Int[] month30 = {4,6,9,11};            const int year = 1900;            const int MONTH = 1;            const int day = 1;            The ***************************************** result + = (year-year) * 365 is calculated first for the whole year; for (int i = year, i < year; i++) {if (Isleapyear (i)) {+ +)                Result            }}//Calculate month ******************************************** if (Month > month) {for (int i = month; i < month; i++) {if (i = = 2 &&am P                    Isleapyear (year)) {result + = 29;        } else {result + = 28;            } if (month30.                    Contains (i)) {result + = 30; } if (month31.                    Contains (i)) {result + = 31;                            }}}} else {//month is equal, it is not possible to have a month less than 1, do not need to do any operations, calculate the direct calculation on the following day            }//Calculation day ********************************* result + = Day-day;        return result; }

2, the number of days difference to 7, for example, the difference between 1900-01-07 and 1900-01-01 7 days, then take the remainder to get 0, the explanation is Sunday,

If it is 1900-01-08, then the remainder is 1, and 1900-01-01 is the same as one weeks, i.e. Monday

        <summary>///day of the Week///</summary>//        <param name= "Days" > Day difference </param> //        <param name= "Weekint" > after the remainder </param>//        <returns></returns>        Private static string getweekday (int days,out int weekint)        {            Dictionary<int, string> weekdic = new dictionary< int, string> ();            Weekdic.add (1, "Monday");            Weekdic.add (2, "Tuesday");            Weekdic.add (3, "Wednesday");            Weekdic.add (4, "Thursday");            Weekdic.add (5, "Friday");            Weekdic.add (6, "Saturday");            Weekdic.add (7, "Sunday");            Weekint = days% 7 = = 0? 7:days% 7;            return weekdic[weekint];                    }

3. Output results

                Console.WriteLine ("Please enter a date:");                String datestr = Console.ReadLine ();                string[] Datearray = Datestr.split ('-');                int year = Int. Parse (Datearray[0]);                int month = Int. Parse (datearray[1]);                int day = Int. Parse (datearray[2]);                int days = GetDays (year, month, day);                int weekint = 0;                Console.WriteLine ("Distance 1900-1-1, with" + Days + "Day of the Time");                Console.WriteLine ("This day is" + getweekday (days, out Weekint));

Can be used on the JS calculation week.

Day of the week to get the date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.