Solved

The date of a previous date in VB.NET

Posted on 2011-03-05
13
359 Views
Last Modified: 2012-05-11
I know how to get only the date of the System date, but not the date of a previous date.

Sounds confusing, but that's my problem.
0
Comment
Question by:gbmcneil
  • 8
  • 3
  • 2
13 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Hi,

what you mean by "date of a previous date"?

If this means that you want to get the date of yesterday will be like this

Datetime.Today.AddDays(-1)

This will returns the "Date" of yesterday


0
 

Author Comment

by:gbmcneil
Comment Utility
Thanks for responding gamarrojqq -

No. I want to extract the date of a previous known date/time.

            prevdatetime = #2/28/2011 8:45:00 PM#
            prevday = nowdatetime.DayOfWeek.ToString     (Gives me "Monday")
            prevdate = nowdatetime      ?                               (Looking for "2/28/2011")
0
 

Author Comment

by:gbmcneil
Comment Utility
I should mention that the date variables are previously dimmed as Objects.
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Ok, using your example

prevdatetime = #2/28/2011 8:45:00 PM#
prevday = nowdatetime.DayOfWeek.ToString     (Gives me "Monday")
prevdate = nowdatetime.ToString("MM/dd/yyyy")                               (Looking for "2/28/2011
0
 

Author Comment

by:gbmcneil
Comment Utility
A slight error in my example: nowdatetime should have been prevdatetime.

Nonetheless, when I run this I get "Conversion from string "MMddyyyy" to type 'Integer' is not valid."

Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object

prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToString     (Gives me "Monday")
prevdate = prevdatetime.ToString("MM/dd/yyyy")                               (Looking for "2/28/2011

What am I doing wrong here?
0
 

Author Comment

by:gbmcneil
Comment Utility
I got the following code to work -

Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object

prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToString         (Gives me "Monday")
prevdate = prevdatetime.ToShortDateString             (Gives me "2/28/2011)

But, I will credit you with the answer if you can get your approach to work. We are all trying to learn here.


0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Declare your variables with the correct data types:
Dim prevdatetime As DateTime
        Dim prevday As String
        Dim prevdate As String

Open in new window

0
 

Author Comment

by:gbmcneil
Comment Utility
Hi Idle -

Embelishing on your suggestion, why wouldn't I use this code instead?

Dim prevdatetime As DateTime
Dim prevday As DateTime
Dim prevdate As DateTime

prevdatetime = #2/28/2011 8:45:00 PM#
prevday = CDate(prevdatetime.DayOfWeek.ToString)         (Gives me "Monday")
prevdate = CDate(prevdatetime.ToShortDateString)             (Gives me "2/28/2011)

Wouldn't that put me in a position to compare these variables with other like variables if I wanted to? In otherwords, as a general rule in VB.NET dim all date and time variables as DateTime. Would I not dim them all as Variants in VB 6.0?  

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
This line:

    prevday = CDate(prevdatetime.DayOfWeek.ToString)

Will cause an exception.  How do you convert "Monday" to a Date?



This line:

    prevdate = CDate(prevdatetime.ToShortDateString)

Could be replace with:

    prevdate = prevdatetime.Date

Why convert to a String just to convert back to a date?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
"as a general rule in VB.NET dim all date and time variables as DateTime. "

Sure...because you can extract just the Date with DateTime.Date, and extract just the Time with DateTime.TimeOfDay.
http://msdn.microsoft.com/en-us/library/system.datetime.date(VS.71).aspx
http://msdn.microsoft.com/en-us/library/system.datetime.timeofday(VS.71).aspx
0
 

Author Comment

by:gbmcneil
Comment Utility
You're saying don't make them separate variables. Just deal with them in their "attributed" form.

I think I got it now.
0
 

Author Comment

by:gbmcneil
Comment Utility
And, can you compare ThisDateTime.TimeOfDay with ThatDateTime.TimeOfDay?

That is:

If ThisDateTime.TimeOfDay > ThatDateTime.TimeOfDay Then
     'blah, blah, blah...
End If


 
0
 

Author Closing Comment

by:gbmcneil
Comment Utility
You are a big help as usual, Idle.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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

13 Experts available now in Live!

Get 1:1 Help Now