CSharp examples for System:DateTime Day
If given object is date, returns date of first day of the week representing by the given date.
/******************************************************************** * FulcrumWeb RAD Framework - Fulcrum of your business * * Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED * * * * THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED * * FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE * * COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE * * AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT * * AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE * * AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS. * ********************************************************************/ using System.Globalization; using System;//w w w.j a va 2 s. c o m public class Main{ //------------------------------------------------------------------------- /// <summary> /// If given object is date, returns date of first day of the week representing by the given date. /// </summary> /// <param name="dateObj">date to get first day of week for</param> /// <returns>date of first day of the week</returns> static public object GetFirstWeekDay(object dateObj) { DateTime date; if (Parse(dateObj, out date)) { return GetFirstWeekDay(date); } return dateObj; } //------------------------------------------------------------------------- /// <summary> /// Returns date of first day of the week representing by the given date. /// </summary> /// <param name="date">date to get first day of week for</param> /// <returns>date of first day of the week</returns> static public DateTime GetFirstWeekDay(DateTime date) { int firstWeekDay = Convert.ToInt32(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek); int dateWeekDay = Convert.ToInt16(date.DayOfWeek); int difference = dateWeekDay - firstWeekDay; if (difference < 0) { difference = 7 + difference; } return difference > 0 ? date.AddDays(-difference) : date; } //------------------------------------------------------------------------- /// <summary> /// Parses datetime value using standard format. /// </summary> /// <param name="o">value to parse</param> /// <returns>parsed datetime value</returns> static public DateTime Parse(object o, DateTime defaultValue) { DateTime d; if (Parse(o, out d)) { return d; } else { return defaultValue; } } //-------------------------------------------------------------------------- /// <summary> /// Parses datetime value using standard format. /// </summary> /// <param name="o">value to parse</param> /// <returns>parsed datetime value</returns> static public bool Parse(object o, out DateTime d) { d = DateTime.MinValue; if (CxUtils.IsEmpty(o)) { return false; } if (o is DateTime) { d = (DateTime)o; return true; } try { d = Convert.ToDateTime(o); return true; } catch { return false; } } }