How to find the start date of week from a given date? - vba

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.

Related

How to have Word VBA TypeText 45 days after today's date, but have it display the following Monday?

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.

Unable to format a timestamp to a date in vba

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

Default date is not what I expected for a Date variable initialized with time only

With a Date variable, apparently it defaults to January 1, 0001 if you don't enter a date in a date/time literal.
However, if I choose to display that default date, it shows the date December 30, 1899 which isn't even an acceptable date in Excel. Here's the code:
Sub TimeOfBirth()
Dim BirthTime As Date
BirthTime = "2:00:00 PM"
MsgBox Format(BirthTime, "yyyy-mm-dd")
End Sub
Can anyone explain this? I get that VBA stores a much greater range of dates, but I don't understand why December 30, 1899 is being displayed?
What's the math/logic there?
Excel treats date as a number. Since you provide only Time, he assumes that Date = 0.
And Date 0 is December 30, 1899.
Edit
To answer your question from comments:
Date defaults to January 1, 0001 in VB.NET.
Date default to December 30, 1899 in VBA and is stored as 0.
Date = 1 not equals to January 1, 1900, but to December 31, 1899. January 1, 1900 is equal to 2. Check code below.
Sub TimeOfBirth()
MsgBox Format(0, "yyyy-mm-dd") '1899-12-30
MsgBox Format(1, "yyyy-mm-dd") '1899-12-31
MsgBox Format(2, "yyyy-mm-dd") '1900-01-01
End Sub

Date and time range where Date() and Date()-1 can't be use in access query

I am trying to set up a query that pulls data from a date field. The date range (for example) that I need is from 3:40 PM of yesterday to today's date up to 3:40 PM. In other words my day does not star at midnight, so the function Date() can't cover it.
I have set up the query as follows:
Between #2/5/2018 3:40:00 PM# And #2/6/2018 3:39:59 PM#
in a field formatted for general Date (mm/dd/yyyy h:mm:ss AM or PM). With this I would have to change the query every day.
I would like to be able to use the function Date() & Date()-1 to replace today's date (Date()) and Yesterday (Date()-1). How can I do it?
I have also tried having two fields one for the Date (formatted as short date mm/dd/yyyy) when the order was entered, and the time the order was entered (formatted for general Date (mm/dd/yyyy h:mm:ss AM or PM). However when I use the function Date() on the date field and >#h:mm:ss# on the Time field the query yields 0 records.
You can do simple calculus with dates and times. Try the following:
Between Date() - 1 + #3:40:00 PM# And Date() + #3:39:59 PM#

VBA return Meridiem (AM/PM) from current Date

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.