Solved

Get date (Previous Monday and Sunday)

Posted on 2015-02-16
3
220 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

17 Experts available now in Live!

Get 1:1 Help Now