Return day of week as integer - CSharp System

CSharp examples for System:DateTime Day

Description

Return day of week as integer

Demo Code

/* //w  w  w  .  ja  v a2  s. co m
 * DateFormatter.cs
 * 
 * Copyright (c) 2009, Elze Kool (http://www.microframework.nl)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * 
 * MS   09-03-23    changed using culture fields instead of English hard-coded
 * 
 * 
 */
using System.Globalization;
using Microsoft.SPOT;
using System;

public class Main{
        // Return day of week as integer
        // Used this funtion to make sure sunday is 0 and monday = 6 (with offset=0)
        private static int _dayofweek(DayOfWeek d, int offset)
        {
            int ret = 0;
            switch (d)
            {
                case DayOfWeek.Sunday:
                    ret = 0;
                    break;

                case DayOfWeek.Monday:
                    ret = 1;
                    break;

                case DayOfWeek.Tuesday:
                    ret = 2;
                    break;

                case DayOfWeek.Wednesday:
                    ret = 3;
                    break;

                case DayOfWeek.Thursday:
                    ret = 4;
                    break;

                case DayOfWeek.Friday:
                    ret = 5;
                    break;

                case DayOfWeek.Saturday:
                    ret = 6;
                    break;
            }

            ret += offset;
            while (ret > 6) ret -= 6;
            return ret;
        }
}

Related Tutorials