Date problem ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony B

    Date problem ?

    I'm trying to write a simple vb function that takes a current date and
    calculates for the first day of the month of that date what day of the week
    it is.

    I thought I could take a date object dt, use dt.Days() to return how many
    days into the month, then create a date object equal to the 1st of the
    month by subtracting the number of days from dt, and then use the dayofweek
    method to find out what I need.
    My difficulty is how create a date object that is the first day of this
    month. The subtract methods need a date object or a timespan object as an
    argument, but I cannot see how to set a timespan object to a certain number
    of days ?
    Am I being stupid here and missing the obvious ?


  • Armin Zingler

    #2
    Re: Date problem ?

    "Tony B" <tonyb@imagepro c.comschrieb
    I'm trying to write a simple vb function that takes a current date
    and calculates for the first day of the month of that date what day
    of the week it is.
    >
    I thought I could take a date object dt, use dt.Days() to return how
    many days into the month, then create a date object equal to the
    1st of the month by subtracting the number of days from dt, and then
    use the dayofweek method to find out what I need.
    My difficulty is how create a date object that is the first day of
    this month. The subtract methods need a date object or a timespan
    object as an argument, but I cannot see how to set a timespan object
    to a certain number of days ?
    Am I being stupid here and missing the obvious ?

    Private Shared Function WeekDayOfFirstD ayInCurrentMont h() _
    As DayOfWeek

    Dim Today As Date = DateTime.Today

    Return Today.AddDays(-Today.Day + 1).DayOfWeek

    End Function

    Usage:
    MsgBox( _
    Globalization.D ateTimeFormatIn fo.CurrentInfo. GetDayName( _
    WeekDayOfFirstD ayInCurrentMont h _
    ) _
    )


    Armin

    Comment

    • Steve Gerrard

      #3
      Re: Date problem ?

      Tony B wrote:
      My difficulty is how create a date object that is the first day of
      this month.
      I have always used something like
      Dim FirstOMonth As Date = New Date(Today.Year , Today.Month, 1)


      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Date problem ?

        Tony,
        Am I being stupid here and missing the obvious ?
        Not wanting to be rude, yes.

        Have the next time a look at the overloaded functions of methods (not only
        this one), that will help you a lot.

        Cor

        Comment

        Working...