[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get date (Previous Monday and Sunday)

Posted on 2015-02-16
3
Medium Priority
?
484 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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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