Hi all

This is not really a coding question but rather I'm looking for the correct term. I was about to write something so I could decorate field as adate and then use softdates or date shortcuts etc like below but on googling for some inspiration I suddenly realised I have no idea what the correct term is for this, if there is one? softdates does not seem to exist so no idea where I got this?

T = Today
Y = Yesterday
T+1 = Today add 1 day
T+1M = Today’s Date + 1 Month
T+1Y = Today’s Date + 1 Year

FDCM = First Day Current Month
LDCM = Last Day Current Month

Recommended Answers

All 3 Replies

That is commonly called "relative dates". I don't know if this will help you any:

using System;
using System.ComponentModel;

namespace XXX.Core
{
  public enum RelativeDate
  {
    [Description("Today")]
    Today,
    [Description("Yesterday")]
    Yesterday,
    [Description("Tomorrow")]
    Tomorrow,

    [Description("First day of previous Week")]
    FirstDayPreviousWeek,
    [Description("First day of previous Month")]
    FirstDayPreviousMonth,
    [Description("First day of previous Year")]
    FirstDayPreviousYear,

    [Description("First day of current Week")]
    FirstDayCurrentWeek,
    [Description("First day of current Month")]
    FirstDayCurrentMonth,
    [Description("First day of current Year")]
    FirstDayCurrentYear,

    [Description("First day of next Week")]
    FirstDayNextWeek,
    [Description("First day of next Month")]
    FirstDayNextMonth,
    [Description("First day of next Year")]
    FirstDayNextYear,

    [Description("Last day of previous Week")]
    LastDayPreviousWeek,
    [Description("Last day of previous Month")]
    LastDayPreviousMonth,
    [Description("Last day of previous Year")]
    LastDayPreviousYear,

    [Description("Last day of current Week")]
    LastDayCurrentWeek,
    [Description("Last day of current Month")]
    LastDayCurrentMonth,
    [Description("Last day of current Year")]
    LastDayCurrentYear,

    [Description("Last day of next Week")]
    LastDayNextWeek,
    [Description("Last day of next Month")]
    LastDayNextMonth,
    [Description("Last day of next Year")]
    LastDayNextYear,
  }
  /* -------------------------------------------------------------------- */
  public static class Dates
  {
    public static DateTime FirstDayOfMonth()
    {
      return FirstDayOfMonth(DateTime.Today).Date;
    }
    public static DateTime FirstDayOfMonth(DateTime dt)
    {
      return dt.AddDays(-1 * (dt.Day - 1)).Date;
    }
    public static DateTime FirstDayOfWeek()
    {
      return FirstDayOfWeek(DateTime.Today).Date;
    }
    public static DateTime FirstDayOfWeek(DateTime dt)
    {
      int dw = (int)dt.DayOfWeek;
      return dt.AddDays(-1 * dw).Date;
    }
    public static DateTime FirstDayOfYear()
    {
      return FirstDayOfYear(DateTime.Today).Date;
    }
    public static DateTime FirstDayOfYear(DateTime dt)
    {
      return FirstDayOfMonth(dt.AddMonths(-1 * (dt.Month - 1))).Date;
    }
    public static DateTime RelativeDateToAbsolute(RelativeDate RelativeType)
    {
      return RelativeDateToAbsolute(RelativeType, DateTime.Today).Date;
    }
    /* -------------------------------------------------------------------- */
    public static DateTime RelativeDateToAbsolute(RelativeDate RelativeType, DateTime dt)
    {
      switch (RelativeType)
      {
        case RelativeDate.FirstDayCurrentMonth:
          return FirstDayOfMonth(dt).Date;
        case RelativeDate.FirstDayCurrentWeek:
          return FirstDayOfWeek(dt).Date;
        case RelativeDate.FirstDayCurrentYear:
          return FirstDayOfYear(dt).Date;
        case RelativeDate.FirstDayNextMonth:
          return FirstDayOfMonth(dt.AddMonths(1)).Date;
        case RelativeDate.FirstDayNextWeek:
          return FirstDayOfWeek(dt.AddDays(7)).Date;
        case RelativeDate.FirstDayNextYear:
          return FirstDayOfYear(dt.AddYears(1)).Date;
        case RelativeDate.FirstDayPreviousMonth:
          return FirstDayOfMonth(dt.AddMonths(-1)).Date;
        case RelativeDate.FirstDayPreviousWeek:
          return FirstDayOfWeek(dt.AddDays(-7)).Date;
        case RelativeDate.FirstDayPreviousYear:
          return FirstDayOfYear(dt.AddYears(-1)).Date;
        case RelativeDate.LastDayCurrentMonth:
          return FirstDayOfMonth(dt.AddMonths(1)).AddDays(-1).Date;
        case RelativeDate.LastDayCurrentWeek:
          return FirstDayOfWeek(dt).AddDays(6).Date;
        case RelativeDate.LastDayCurrentYear:
          return FirstDayOfYear(dt.AddYears(1)).AddDays(-1).Date;
        case RelativeDate.LastDayNextMonth:
          return FirstDayOfMonth(dt.AddMonths(2)).AddDays(-1).Date;
        case RelativeDate.LastDayNextWeek:
          return FirstDayOfWeek(dt.AddDays(14)).AddDays(-1).Date;
        case RelativeDate.LastDayNextYear:
          return FirstDayOfYear(dt.AddYears(2)).AddDays(-1).Date;
        case RelativeDate.LastDayPreviousMonth:
          return FirstDayOfMonth(dt).AddDays(-1).Date;
        case RelativeDate.LastDayPreviousWeek:
          return FirstDayOfWeek(dt).AddDays(-1).Date;
        case RelativeDate.LastDayPreviousYear:
          return FirstDayOfYear(dt).AddDays(-1).Date;
        case RelativeDate.Today:
          return dt.Date;
        case RelativeDate.Tomorrow:
          return dt.Date.AddDays(1).Date;
        case RelativeDate.Yesterday:
          return dt.Date.AddDays(-1).Date;
        default:
          throw new InvalidEnumArgumentException("RelativeType", (int)RelativeType, typeof(RelativeDate));
      }
    }
  }
}
commented: thanks again! +1

Ahh, I knew it would be something easy but all the extra Christmas chocolate must have got to me!

Thanks very much for the code too. That's one less google search!

Thanks!

You're welcome

Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.