Solved

The date of a previous date in VB.NET

Posted on 2011-03-05
13
373 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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