Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

The date of a previous date in VB.NET

Posted on 2011-03-05
13
Medium Priority
?
381 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
[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
  • 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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
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 86

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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