Solved

Get date (Previous Monday and Sunday)

Posted on 2015-02-16
3
201 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 167 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 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 167 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 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 166 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now