I'm trying to format a timestamp 1624373961042 to a date but the way I've tried below always gives me overflow error. I was expecting to get this date 2021-06-22 from that timestamp. How can I make it possible?
Sub getDate()
Dim tDate As Long, timestamp As String
timestamp = "1624373961042"
tDate = Int(timestamp) / 1000
MsgBox Format(tDate, "yyyymmdd")
End Sub
You need to divide the number by the number of milliseconds in a day: 86400000
then add the number of days from 12/31/1899 and 1/1/1970: 25569
Sub getDate()
Dim tDate As Long, timestamp As String
timestamp = "1624373961042"
tDate = Int((CDbl(timestamp) / 86400000) + 25569)
MsgBox Format(tDate, "yyyymmdd")
End Sub
Related
I'm trying to insert a future date for a visit, which will be 20 days after the current date, but if that date isn't on a Monday, I need it to be for the following Monday.
My code is currently:
.TypeText "We will visit your location the week of "
.TypeText Text:=Format(Date + 20, "mmmm d, yyyy, ")
That falls on Saturday, May 14th. I would like it to display The 16th, since that is the following Monday.
Add this function
Function MondayOnOrAfter(StartDate As Date) As Date
Dim ReturnDate As Date
ReturnDate = StartDate
Do While Weekday(ReturnDate) <> vbMonday
ReturnDate = ReturnDate + 1
Loop
MondayOnOrAfter = ReturnDate
End Function
and call it like this
.TypeText Text:=Format(MondayOnOrAfter(Date + 20), "mmmm d, yyyy, ")
It adds one day at a time until it's Monday. It would be possible to solve this without a loop, but that code wouldn't be pretty.
Year(Now) returns the current Year
Month(Now) returns the current Month
Day(Now) returns the current Day
Hour(Now) returns the current Hour
Minute(Now) returns the current Minute
How do you return AM/PM?
try using below
Format(Now, "am/pm")
Format(TimeValue(Now), "hh:mm AM/PM").
Change AM/PM to am/pm - A/P to a/p. for lowercase-uppercase display.
I have a concern is that I want from a given date to retrieve the start date of the week for example: 15/04/2015 So the beginning of the week will: 13/04/2015 (for me the beginning of the week is Monday).
thanks
Try this :-)
Dim FirstDayInWeek, LastDayInWeek As Variant
Dim dtmDate As Date
dtmDate = "15/04/2015"
The begin date of week:
FirstDayInWeek = dtmDate - Weekday(dtmDate, vbUseSystem) + 1
MsgBox FirstDayInWeek
The end date of Week
LastDayInWeek = dtmDate - Weekday(dtmDate, vbUseSystem) + 7
MsgBox LastDayInWeek
Try this Formula:-
=A1-WEEKDAY(A1,2)+1
Where A1 contains your Input Date.
I want to get timestamp of a different country in VBA. Is there any direct function or way to get it? For example, I am in India working for Mexico and I want to do certain task based on Mexican Time. I was able to get it by splitting the timestamp and manipulating it but could not consider daylight saving in it. Is there any simple solution than writing a big user-defined function?
Know your time zone offset & your client's time zone offset, then use that to calculate the difference
Dim IndiaTZ as single
Dim MexicoTZ as single
Dim MyTime as date
Dim UTC as date
Dim MexicoTime as date
IndiaTZ = 5.5
MexicoTZ = -4
'note this assumes that they're in Eastern time,
'Mexico also covers Central, Mountain & Pacific at -5, -6 & -7.
'You'll need to figure out which one you need.
MyTime = Now
'need to invert the offset to get from India to UTC
UTC = datediff("h", mytime, IndiaTZ * -1)
'need to invert the offset to get from UTC to Mexico
MexicoTime = Datediff("h", UTC, MexicoTZ * -1)
I'm using TimeSpan function in VB.NET to get the total hours in a billing month.
I have 30 days in a billing month, so supposedly, I have 720 intervals. Using Timespan normally, I would get 696 intervals. But I add a day in my "todate" in order to get my correct total hours.
This is my code:
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.AddDays(1).Subtract(fromdate)
totalhours = TS.TotalHours
However, I don't think that manipulating my input date (by adding a day) is the best practice. Is there any way to configure the timespan function to get the inclusive total hours?
That is because you did not supply the time range. The date alone will be 12AM for the time part.
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014 11:59:59 PM#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
That will be 1 hours shy.
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #12-01-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
Will get the correct amount since it is inclusive of time between the two dates.