Solved

The date of a previous date in VB.NET

Posted on 2011-03-05
13
369 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
ID: 35044572
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
ID: 35044611
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
ID: 35044652
I should mention that the date variables are previously dimmed as Objects.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35044657
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
ID: 35044884
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
ID: 35044932
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35044971
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
ID: 35047230
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
ID: 35047278
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
ID: 35047283
"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
ID: 35047527
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
ID: 35047545
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
ID: 35048280
You are a big help as usual, Idle.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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