?
Solved

Get date (Previous Monday and Sunday)

Posted on 2015-02-16
3
Medium Priority
?
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 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