[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get date (Previous Monday and Sunday)

Posted on 2015-02-16
3
Medium Priority
?
561 Views
Last Modified: 2015-02-19
I want to get previous week dates (Monday and Sunday).
 Sample. today is 2/16/2015 (or any day in that same week), Start_date should be 2/09/2015 and End_date should be 2/15/2015.
 
 Is this correct way?
 
 Dim curerntDate As DateTime = DateTime.Now
 Dim delta As Integer = DayOfWeek.Monday - curerntDate.DayOfWeek
 Dim monday As DateTime = curerntDate.AddDays(delta)
 

ASPxDateEdit_start_date.[Date] = monday.AddDays(-7)  
ASPxDateEdit_end_date.[Date] = monday.AddDays(-1)
0
Comment
Question by:VBdotnet2005
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 668 total points
ID: 40612831
The best way is to see if you get the good results, using different dates to perform the tests, specially the dates around your limits, such as a date on a Monday, and another on a Friday. Do not perform the tests only on the current week, try with other dates too. If the results are good (they are, I have checked), and if the code is a simple as the one you present, you can assume that it is correct.

One thing however, do not use DateTime.Now when dealing with a date and you do not need the time. This can lead to problems in some situations.

Also, in VB, you are better to use Date instead of DateTime. They are the same time under the hood, but because Date is a VB reserved work (in blue), the compiler sometimes do a better job than when you use the equivalent .NET class (in aqua).
0
 
LVL 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 668 total points
ID: 40612855
        Dim curerntDate As DateTime = DateTime.Now
        Dim dayDiff As Integer = curerntDate.DayOfWeek - DayOfWeek.Monday

        MessageBox.Show(curerntDate.AddDays(-dayDiff - 7).ToString)
        MessageBox.Show(curerntDate.AddDays(-dayDiff - 1).ToString)
        'ASPxDateEdit_start_date.[Date] = curerntDate.AddDays(-dayDiff - 1).ToString
        'ASPxDateEdit_end_date.[Date] = curerntDate.AddDays(-dayDiff - 7).ToString

Open in new window


Yes, your solution works fine. Above is an alternate solution.

Mike
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 664 total points
ID: 40618446
It will fail for Sundays
You need a little more:

Dim currentDate As DateTime = DateTime.Today
Dim mondayThis As DateTime
Dim sundayThis As DateTime
Dim mondayPrevious As DateTime
Dim sundayPrevious As DateTime

mondayThis = currentDate.AddDays((DayOfWeek.Monday - currentDate.DayOfWeek - 7) Mod 7)
sundayThis = currentDate.AddDays((DayOfWeek.Sunday - currentDate.DayOfWeek - 7) Mod 7)

mondayPrevious = mondayThis.AddDays(-7)
sundayPrevious = sundayThis.AddDays(-7)

/gustav
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question