Issue with Counter and a Date - vb.net

I am havving a little issue with a date like the 1AM bellow but if I use a date like this it works fine "MAR 28, 2014 3PM" for some reason I get a "-17:49" while using the 1AM.. why is this?
Dim myTime As String = "MAR 26, 2014 1AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(myTime)
Dim ts As New TimeSpan
ts = date2 - date1
If ts.Days > 0 Then
TextBox.Text = ts.Days & ":" & ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
ElseIf ts.Hours > 0 Then
TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
Else
TextBox.Text = ts.Minutes & ":" & ts.Seconds
If ts.Minutes = 2 And ts.Seconds < 30 Then
doStop = True 'we are 2:30 minutes away from closing
End If
If ts.Minutes < 15 Then
'TextBox.text = "Ending Soon"
End If
End If

You are converting the current date/time to string, then auto-converting it back to a date value immediately on that second line.
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
^^^^^^^^ this converts date to string
^^^^^^^^ this converts it back due to implicit casting
But since you are using hh as the format, you get only the 12-hour value of the date, so some information about the date value is lost.
For example, it is currently 12:34 AM EST where I am testing the code, but if I dump out the contents of date1 immediately after that second line, it has already become 12 hours later!
3/26/2014 12:34:41 PM
When you want to work with date values and do math on them, leave them as date values. Don't convert to string until you're ready to display something to the user. So just use:
Dim date1 as DateTime = System.DateTime.Now

Related

VBA, Dynamically pickup a date

I have a file I want to pickup from the previous wednesday June 10th. I will be running my code today (Sunday June 14th). However this will reoccur every week.
Is there a way I can make my code dynamic enough to pickup the previous wednesday date?
Here is my code Set wbTarget = Workbooks.Open("C:\extract\Business_Report_20200527.xlsx")
Can I alter that path to dynamically pickup a date?
Thank you.
Try this:
Dim today As Date
Dim IntervalType As String
Dim FilePath As String
today = Date()
IntervalType = "d"
While Weekday(today) <> vbWednesday
today = DateAdd(IntervalType, -1, today)
Wend
FilePath = "C:\extract\Business_Report_" + Format(today, "yyyymmdd") + ".xlsx"
Set wbTarget = Workbooks.Open(FilePath)
Another way to do it:
Sub Test()
MsgBox "Last Wednesday: " & PreviousWednesday & vbCr & _
"Wednesday prior to 9th June: " & PreviousWednesday(DateValue("9 June 2020")) & vbCr & _
"Last Wednesday formatted: " & Format(PreviousWednesday, "yyyymmdd")
'Your code:
'Set wbTarget = Workbooks.Open("C:\extract\Business_Report_" & Format(PreviousWednesday, "yyyymmdd") & ".xlsx")
End Sub
Public Function PreviousWednesday(Optional CurrentDate As Date) As Date
If CurrentDate = 0 Then CurrentDate = Date
PreviousWednesday = (CurrentDate - Weekday(CurrentDate, vbMonday) + 1) - 5
End Function

Using Now() for Foreign Countries

I'm trying to convert different date formats into an appropriate SQL DateTime format and am getting errors.
I have a client's machine that is setup with the following date format, "dd-mm-yyyy" (Localization is India)
When using Now() within VB.Net it parses the month and days backwards. I've tried the following code but get conversion errors.
Dim Month As String = Nothing
Dim Year As String = Nothing
Dim Day As String = Nothing
Dim Hour As String = Nothing
Dim Minute As String = Nothing
Dim Second As String = Nothing
Dim strDate As String = Nothing
Dim Date_to_SQL_DateTime As Date
Month = DatePart(DateInterval.Month, Now()).ToString
Year = DatePart(DateInterval.Year, Now())
Day = DatePart(DateInterval.Day, Now())
Hour = DatePart(DateInterval.Hour, Now())
Minute = DatePart(DateInterval.Minute, Now())
Second = DatePart(DateInterval.Second, Now())
strDate = Month & “/” & Day & “/” & Year & “ “ & Hour & “:” & Minute & “:” & Second
Date_to_SQL_DateTime = Date.ParseExact(strDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture)
strDate returns
1/24/2017 23:17
Error I'm receiving is
String was not recognized as a valid Datetime
Try below. Need to make sure parameters are of same format as specified in the ParseExact function.
Dim Month As String = Nothing
Dim Year As String = Nothing
Dim Day As String = Nothing
Dim Hour As String = Nothing
Dim Minute As String = Nothing
Dim Second As String = Nothing
Dim strDate As String = Nothing
Dim Date_to_SQL_DateTime As Date
Month = DatePart(DateInterval.Month, Now()).ToString("00")
Year = DatePart(DateInterval.Year, Now()).ToString("0000")
Day = DatePart(DateInterval.Day, Now()).ToString("00")
Hour = DatePart(DateInterval.Hour, Now()).ToString("00")
Minute = DatePart(DateInterval.Minute, Now()).ToString("00")
Second = DatePart(DateInterval.Second, Now()).ToString("00")
strDate = Month & "/" & Day & "/" & Year & " " & Hour & ":" & Minute & ":" & Second
Date_to_SQL_DateTime = DateTime.ParseExact(strDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture)

VBA code to convert a string format in to date format

I have a date format in excel which is
Fri Aug 22 01:10:24 EDT 2014
I want to convert it into 08/22/2014 1:10 am format using VBA coding.
Try this:
Function CONVDATE(mydate) As Date
Dim arr
arr = Split(mydate)
CONVDATE = arr(1) & " " & arr(2) & " " & arr(5) & " " & arr(3)
End Function
This should work provided the expression is standard.
It will return a Date then you can then use the Custom Format in excel to get your desired format. HTH
Result:
You can use it as a Worksheet Function and enter as normal formula.

Setup a count down based on a time

I would like to know how to setup a count down based on a time.. for example its 6pm and there is a time like 11PM, how would I have a countdown that I click a button no timer, that shows me you have 5 hours left etc.
Dim myTime As String = "7AM"
Dim date1 As DateTime = Date.Now
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)
Dim ts As New TimeSpan
ts = date2 - date1
TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
If I got it right, do you want to get the remaining time by clicking a button? which also means that you have to click it everytime for it to update?
Dim myTime As String = "7AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss tt, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)
Dim ts As New TimeSpan
If date2 < date1 Then
date2 = date2.AddDays(1)
End If
ts = date2 - date1
MsgBox(ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds)
That would return a time with 16:30:00 with hh:mm:ss format.

VBA Macro and changing date format to mmm-yy

I am having problems with an excel macro (VBA) that is meant to grab a date from an excel spreadsheet, subtract one month and reformat it to MMM-YY. Basically I want to take 3/31/2013 and convert it to Feb-13
Here is my code:
Dim ReportDate As Date
ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm") & "-" & Format(ReportDate, "yy")
Debug.Print prevMonth
The result I get is 2/31/2013-13
So I tried changing the prevMonth variable:
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm-yy")
But got just 2/31/2013 again
I tried to declare prevMonth as an Integer or a Date but I get a type mismatch error. I can only declare it as a String but it still doesn't help the program.
Thanks in advance for your help.
Try this
Sub Demo()
Dim ReportDate As Date
Dim prevMonth As Date
ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
prevMonth = DateAdd("m", -1, ReportDate)
Debug.Print Format(prevMonth, "mmm-yy")
End Sub