How to compare day of the week after adding the days to the already present date and if day of the week is other than friday , than get the next nearest friday and print the day

I need to get the day of the week after adding a fix number of days to the already printed date and also get the day of the week after adding number of days and if the day is not Friday than allocate the date for the next nearest Friday and print it in the excel
the number of days to be added is say 30 to every date in the sheet.

Hello @Kunal_Jain ,

  • For retrieving the day of a week you could use:
 ' Get weekday number using Visual Basic Weekday function
        Console.WriteLine(Weekday(dateValue))                 ' Displays 4
        ' Compare with .NET DateTime.DayOfWeek property
        Console.WriteLine(dateValue.DayOfWeek)                ' Displays 3

        ' Get weekday name using Weekday and WeekdayName functions
        Console.WriteLine(WeekdayName(Weekday(dateValue)))    ' Displays Wednesday

        ' Change culture to de-DE
        Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
        Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
        ' Get weekday name using Weekday and WeekdayName functions
        Console.WriteLine(WeekdayName(Weekday(dateValue)))   ' Displays Donnerstag

        ' Restore original culture
        Thread.CurrentThread.CurrentCulture = originalCulture

Source: https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date

  • For adding days you could use:
DateAdd("m", 1, "31-Jan-95")

Source: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function

Hope it clarifies your issue.

Best regards,
Marius

image
Getting this error

All following statements can be used within assign statements

Lets assume we do have a date of datatype: datetime - now or any other variable

getting the dayinweek info: now.DayOfWeek

Adding some days:
grafik

Adding some days and find also the next Friday
grafik

now.AddDays({5,4,3,2,1,0,6}(now.DayOfWeek))
the idea behind is that we do configure to offset to the next Friday within the array and selecting the needed value depending to the day in the week for a particular date

1 Like