Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Date Manipulation In Visual Basic

Posted on 2003-02-26
14
Medium Priority
?
661 Views
Last Modified: 2013-12-04
I'm trying to find the next day of the powerball, It falls on each wednesday and saturday of each week. I want the out put to be in long date format so if it is a Tuesday the program would give back "Wednesday the blah blah blah" if it were thursday the program would give back "Saturday the blah blah blah" and if it is the day the program would give back "Today, Wednesday the blah blah blah" but it also needs to calculate for the month to roll over into the next. Here are some examples of what I need in and what should come out

2/24/03 then the output should be "Wednesday, Febuary 26th 2003"

2/26/03 then the output should be "Today, Wednesday, Febuary 26th 2003"

and if it is
2/27/03 then the output should be "Saturday March 1st 2003"

the last example has been my trickiest because I know Saturday is 2 days away from Thursday (2/27) but the last day of the month is the 28th

I have been working on code and getting close but no cigar, I'm at work and my code is at home so I'll post what I got so far later but just a side note this is all behind the scenes so the user will input nothing so to get the current date I'd like to use Date$ or Now() or something similar. Basiclly a function to add N amount of days to a date and include calculation for month rollovers. I can find the N amount of days away just can't produce a new correct date.
Thanks,
MoDrop - Ascii(13)
0
Comment
Question by:MoDrop
  • 5
  • 5
  • 4
14 Comments
 
LVL 3

Expert Comment

by:MCummings111400
ID: 8027208
Oh, I bet your gonna love this. ;)

Private Sub Command1_Click()
Dim dtCurrent As Date
Dim dtPowerball As Date

    dtCurrent = Text1.Text
    If Weekday(dtCurrent) <= vbWednesday Then
        dtPowerball = dtCurrent + (vbWednesday - Weekday(dtCurrent))
    Else
        dtPowerball = dtCurrent + (vbSaturday - Weekday(dtCurrent))
    End If
   
    Text2.Text = Format(dtPowerball, "long Date")
End Sub
0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 8027229
Oops, didn't read the bottom:

Function NextPowerBall(ByVal dtDate as Date) as Date
Dim dtPowerball As Date

   If Weekday(dtCurrent) <= vbWednesday Then
       dtPowerball = dtDate + (vbWednesday - Weekday(dtCurrent))
   Else
       dtPowerball = dtDate + (vbSaturday - Weekday(dtCurrent))
   End If

   NextPowerBall = dtPowerBall
 End Function
0
 

Author Comment

by:MoDrop
ID: 8028530
When I do This All I Get Returned Is The Current Date Am I Using Your Function Wrong

Private Sub Form_Load()
   msgbox NextPowerBall (Now())
   Unload Me
End Sub

Public Function NextPowerBall(ByVal dtDate as Date) as Date
Dim dtPowerball As Date

  If Weekday(dtCurrent) <= vbWednesday Then
      dtPowerball = dtDate + (vbWednesday - Weekday(dtCurrent))
  Else
      dtPowerball = dtDate + (vbSaturday - Weekday(dtCurrent))
  End If

  NextPowerBall = dtPowerBall
End Function
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8029749
Try


Private Sub Form_Load()
  MsgBox NextPowerBall(Date)
  Unload Me
End Sub

Public Function NextPowerBall(ByVal dtDate As Date) As Date
Dim tmpDate As Date

   tmpDate = dtDate
   Do
      If Weekday(dtDate, vbWednesday) = 0 Or Weekday(dtDate, vbSaturday) = 0 Then
         NextPowerBall = dtDate
         Exit Do
      Else
         tmpDate = DateAdd("d", 1, tmpDate)
      End If
   Loop
End Function
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8029766
Soory never noyticed the part that says you want the return to be of long date format

Replace

NextPowerBall = dtDate
       
with

NextPowerBall = Format(dtDate, "dddd, dd MMMM yyyy")
       
0
 
LVL 5

Accepted Solution

by:
JohnMcCann earned 150 total points
ID: 8029791
I have re read the question and realize you need to return a string in order to get "Today"

So the code

Private Sub Form_Load()
  MsgBox NextPowerBall(Date)
  Unload Me
End Sub

Public Function NextPowerBall(ByVal dtDate As Date) As String
Dim tmpDate As Date

   tmpDate = dtDate
   Do
      If Weekday(dtDate, vbWednesday) = 0 Or Weekday(dtDate, vbSaturday) = 0 Then
         If tmpDate = dtDate Then
            NextPowerBall = "Today " & Format(dtDate, "dddd, dd MMMM yyyy")
         Else
            NextPowerBall = Format(dtDate, "dddd, dd MMMM yyyy")
         End If
         Exit Do
      Else
         tmpDate = DateAdd("d", 1, tmpDate)
      End If
   Loop
End Function
0
 

Author Comment

by:MoDrop
ID: 8030480
Well I Still Found Problems In The Code, Although Now I See All The Wonderful Date Functions After Tinkering With The Code Given To Me And Looking In The MSDN Library I Have Taken JohnMcCann's Function And Fixed It, John I'm Gonna Give You The Points, Yea!!! So Here's The Code Below. Thanks To Everyone Who Helped. If Your're Interested About Why I Needed To Know The Next Powerball Dates Look Below The Code For A Description Of The Program I'm Making, Might Help Your Luck...

Private Sub Form_Load()
    MsgBox NextPowerBall(Date)
    Unload Me
End Sub

Public Function NextPowerBall(ByVal dtDate As Date) As String
    Dim tmpDate As Date
   
    tmpDate = dtDate
    Do
        If Weekday(tmpDate) = 4 Or Weekday(tmpDate) = 7 Then
            If tmpDate = dtDate Then
                NextPowerBall = "Today " & Format(dtDate, "dddd, MMMM dd, yyyy")
            Else
                NextPowerBall = Format(tmpDate, "dddd, MMMM dd, yyyy")
            End If
            Exit Do
        Else
            tmpDate = DateAdd("d", 1, tmpDate)
        End If
    Loop
End Function

I'm Making A Program Which Downloads The Text Files Contating All The Powerball Numbers Ever Picked. After Editing The Text Into Array's I Then Display The 5 Most Picked Numbers (5 Modes) The 2 Averages (The Mean And The Natual Log Mean) And The Median. The Date Was Just To Look Nice For The User It Displays The Next Day It's On And The Estimated Jackpot. It Also Can Generate Random Lotto Balls Or Generate Then With-in A Close Range Of The Modes. Anywho It Has More Options But Is Still In Process. If You Are Interested In Source Code When I'm Done Just Email Me, crolfson@uwm.ed, If You Win Using My Program All I'm Asking For Is A Litte Cash For College, And Maybe A Sweet Car, And Maybe...... Well Lets Just Hope The Program Works :)
0
 

Author Comment

by:MoDrop
ID: 8030554
I Needed To Fix It Up But The Functions Were Just What I Needed, I Don't Know If You Tested It But The Last Code I Posted Works. Thanks A Mill And If Your're Interested In Why I Needed It Look At My Last Post,
Thanks Until Real Time,
MoDrop Ascii(13)
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8030594
NEVER NEVER NEVER USE Weekday(tmpDate) without giving a default say as it will use the systems.  i.e. my system may be set to firstday = Monday and yours SunDay the code wold then produce different results.  That is worth noting for all functions never assume default behavior always explictly declare it.

0
 

Author Comment

by:MoDrop
ID: 8034484
Ahh.. Much Praise. I'm Still Rough With These Date Functions And Didn't Realize The Effect That May Have Until You Brought It Up. Could You Please Check This New Code To See If It Is Good On All Systems No Matter What The System's firstday is equal to. Otherwise I Was Thinking About Changing The First Day To What I Need And Then Back To The Original After The Functions Works. Thanks A Mill,
MoDrop - Ascii(13)

Private Sub Form_Load()
    MsgBox NextPowerBall(Date)
    Unload Me
End Sub

Public Function NextPowerBall(ByVal dtDate As Date) As String
    Dim tmpDate As Date

    tmpDate = dtDate
    Do
        If Weekday(tmpDate, vbWednesday) = 1 Or Weekday(tmpDate, vbSaturday) = 1 Then
            If tmpDate = dtDate Then
                NextPowerBall = "Today " & Format(dtDate, "dddd, MMMM dd, yyyy")
            Else
                NextPowerBall = Format(tmpDate, "dddd, MMMM dd, yyyy")
            End If
            Exit Do
        Else
            tmpDate = DateAdd("d", 1, tmpDate)
        End If
        DoEvents
    Loop
End Function
0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 8035176
MoDrop,
   You were getting back the current day because 2/26/2003 is a wednesday, hence a PowerBall day, If you had tried something other than Now() you would have seen different results.

0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 8035466
MoDrop,
   You were getting back the current day because 2/26/2003 is a wednesday, hence a PowerBall day, If you had tried something other than Now() you would have seen different results.

0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8038539
Sorry I made a mistake

The weekday function always assumes vbSunday as day 1 and for some reason though it returned 0.

But still it is always worth specifiying the behavior you expect.
0
 

Author Comment

by:MoDrop
ID: 8043353
To MCummings,
 About Your Comment:

MoDrop,
  You were getting back the current day because 2/26/2003 is a wednesday, hence a PowerBall day, If you had tried something other than Now() you would have seen different results.

I Tried Changeing My Date On The System. I Still Kept Now() But I Would Change My Day To 2/27/03 And Other Random Dates To Check For It Rolling Over Into The Next Month. The Last Function I Posted Works For All Dates. Thanks Everyone,
MoDrop
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline

580 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