Date after number of days in lua scripting - date

I am new to lua scripting. I have a startDate ("03-05-2014" as "dd-mm-yyyy") and a span in days (2) Can anyone help me how to get the endDate based on the startDate and span?.
Example startDate span endDate
--------- ---- -------
03-05-2014 2 05-05-2014
(dd-mm-yyyy) (dd-mm-2014)

You don't need to do any math here. os.time and os.date will do it for you.
local day, month, year = ("03-05-2014"):match("(%d%d)-(%d%d)-(%d%d%d%d)")
local span = 64
local endtime = os.time({day = day + span, month = month, year = year})
print(os.date("%c", endtime))

I'm not going to write the whole program for you, but here's something you can start with:
Get the day, month and year from the string:
local day, month, year = string.match('03-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
day, month, year = tonumber(day), tonumber(month), tonumber(year)
Use os.time to get the timestamp of a start time. You can
then add 3600 * 24 * 2 seconds (2 days) to get the timestamp of the end time.
Use os.date to formats the string from a timestamp.

This could help you
local dayValue, monthValue, yearValue = string.match('31-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
dayValue, monthValue, yearValue = tonumber(dayValue), tonumber(monthValue), tonumber(yearValue)
now = os.time{year = yearValue, month = monthValue, day = dayValue}
numberOfDays = now + 2 * 24 * 3600
print(os.date("%c",numberOfDays))
dateAfterNumberOfDays = os.date("%a %d %B %Y, %H%p%M",numberOfDays)
print ("\nafter number of days "..dateAfterNumberOfDays) -- give you date after number of days

Related

convert date format yywwd to dd-mm-yy

In Access, I would like to convert a date column in format yywwd to dd-mm-yy. (weekday nr. 1 is monday, and years can only from 2000 and later, so e.g. today (monday 15-06-2020) would be 20251 what I would like to be converted to 15-06-2020.
I'm not much of a coder so honestly asside from messing with Datepart I have not tried a whole lot. Does anyone have suggestion?
It seems that the function 'GetDayFromWeekNumber' mentioned here vba convert week number (and year) to date? could work but how is this used in MSAccess?
Thanks a lot in advance!
I have a function that works in Excel to convert YYWWD to a date, this should be very similar if not identical to the code needed in Access. It is quite verbose so you could probably make it simpler, but at least the calculation steps are clearly set out.
The function assumes the ISO definition of week number - i.e. the first week of the year is the week in which the 4th of Jan falls. The first day of a week is Monday, the last day of a week is Sunday.
Function dateFromYYWWD(yywwd)
Dim sYYWWD As String
Dim sYYYY As String
Dim ww As Integer
Dim d As Integer
Dim fourthOfJan As Date
Dim fourthOfJanWeekday As Integer
Dim week1StartDate As Date
Dim targetWeekStartDate As Date
Dim targetDate As Date
' Convert to string if not already
sYYWWD = "" & yywwd
' Get the year in full
sYYYY = "20" + Left(sYYWWD, 2)
' Get the week number and day in the week
ww = CInt(Mid(sYYWWD, 3, 2))
d = CInt(Right(sYYWWD, 1))
' Calculate the date of 4th Jan in the same year
fourthOfJan = CDate(sYYYY & "-01-04")
' Get the day of week of the 4th Jan
' NOTE - CALCULATES MONDAY AS DAY 1 OF THE WEEK, SUNDAY AS DAY 7
fourthOfJanWeekday = Weekday(fourthOfJan, vbMonday)
' Date of the first day of week #1 in the target year
week1StartDate = fourthOfJan - fourthOfJanWeekday + 1
' First day of the target week
targetWeekStartDate = week1StartDate + (ww - 1) * 7
' Target date
targetDate = targetWeekStartDate + d - 1
dateFromYYWWD = targetDate
End Function
It's as simple as this:
Public Function ConvertFromYYWWD(s) As Date
Dim t&
t = DateSerial(2000 + Mid(s, 1, 2), 1, 1) + 7 * (Mid(s, 3, 2) - 1)
ConvertFromYYWWD = t - Weekday(t, vbMonday) + Mid(s, 5, 1)
End Function
Just place the above function in a code module in the database project.
You mentioned in the comments under your question that the week number is always two digits. I am assuming the the year number is likewise always two digits.
The first task is to split the value:
YWDDate = 20251
Year = YWDDate \ 1000 + 2000
2020
Week = (YWDDate Mod 1000) \ 10
25
Weekday = YWDDate Mod 10
1
Then, as this probably is ISO 8601 week numbering, the year is not the calendar year but the ISO 8601 year, which native VBA knows nothing about, thus a custom function is needed:
' First day of the week.
WeekStart = DateYearWeek(25, 2020, vbMonday)
' Requested day of week (which here is the same)
WeekDate = DateAdd("d", 1 - 1, WeekStart)
The function is not that convoluted:
' Returns the date of Monday for the ISO 8601 week of IsoYear and Week.
' Optionally, returns the date of any other weekday of that week.
'
' 2017-05-03. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateYearWeek( _
ByVal IsoWeek As Integer, _
Optional ByVal IsoYear As Integer, _
Optional ByVal DayOfWeek As VbDayOfWeek = VbDayOfWeek.vbMonday) _
As Date
Dim WeekDate As Date
Dim ResultDate As Date
If IsoYear = 0 Then
IsoYear = Year(Date)
End If
' Validate parameters.
If Not IsWeekday(DayOfWeek) Then
' Don't accept invalid values for DayOfWeek.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If
If Not IsWeek(IsoWeek, IsoYear) Then
' A valid week number must be passed.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If
WeekDate = DateAdd(IntervalSetting(dtWeek), IsoWeek - 1, DateFirstWeekYear(IsoYear))
ResultDate = DateThisWeekPrimo(WeekDate, DayOfWeek)
DateYearWeek = ResultDate
End Function
but - as you can see - it calls some helper functions, which again call other functions, which will be too much to post here.
I can upload it somewhere, if you feel this will provide a solution for you.

Daily interest calculator that's supposed to credit monthly given various user inputs

I'm new to programming and have been working on something to learn Python for the last few days. I'm trying to create a program that calculates daily interest and credits interest monthly given user generated inputs.
The good: The program can add deposits to the initial balance every 14 days as I wanted. The program is able to determine whether an input is a leap year or not. The program is able to determine the daily interest.
The bad: I haven't been able to figure out how to get the program to sum the calculated daily interests for a given month then add it to the beginning balance of the next month.
import datetime
from datetime import date
from datetime import timedelta
import calendar
from decimal import Decimal
def gupta():
print("Enter a start date in YYYY-MM-DD format")
date_entry = input() #records account's start date as string
year, month, day = map(int, date_entry.split('-')) #splits contents of date_entry string variable into separate integers thus rendering date1 variable actionable by datetime.date
date1 = datetime.date(year, month, day) #records account's start date as datetime.date (module?)
print("Enter an end date in YYYY-MM-DD format")
date_entry = input() #records account's end date as string
year, month, day = map(int, date_entry.split('-')) #splits contents of date_entry string variable into separate integers thus rendering date1 variable actionable by datetime.date
date2 = datetime.date(year, month, day) #records account's end date as datetime.date (module?)
print("The starting date is " + str(date1) + " and the ending date is " + str(date2))
print("Enter your initial balance")
initial_balance = Decimal(input()) #requests initial balance (assumes this is amount to be deposited every 14 days)
print("Enter your interest rate (as a decimal)")
interest_rate = Decimal(input()) #records interest rate as a decimal
date1 = date1.toordinal() #changes date1 to ordinal
date2 = date2.toordinal() #changes date2 to ordinal
list_interest = []
for current_date in range(date1, date2 + 1): #cycles through date1 and date2 using a step/iteration of 1
date_spread = current_date - date1
new = current_date + 1
print(date.fromordinal(current_date).strftime('%Y-%m-%d'))
print(list_interest)
if date_spread != 14: #checks to see whether additional deposit is going to be made
current_date = date.fromordinal(current_date)
new = date.fromordinal(new)
current_date_y = str(current_date.strftime("%Y"))
new_m = str(new.strftime("%m"))
current_date_m = str(current_date.strftime("%m"))
current_balance = initial_balance + 0
print(current_balance)
if calendar.isleap(int(current_date_y)):
daily_interest = round(current_balance * (interest_rate / 366), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
else:
daily_interest = round(current_balance * (interest_rate / 365), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
current_date = date.toordinal(current_date)
elif date_spread == 14: #checks to see whether additional deposit is going to be made
current_date = date.fromordinal(current_date)
new = date.fromordinal(new)
current_date_y = str(current_date.strftime("%Y"))
new_m = str(new.strftime("%m"))
current_date_m = str(current_date.strftime("%m"))
current_balance = initial_balance + 150
initial_balance = current_balance
print(current_balance)
if calendar.isleap(int(current_date_y)):
daily_interest = round(current_balance * (interest_rate / 366), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
else:
daily_interest = round(current_balance * (interest_rate / 365), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
current_date = date.toordinal(current_date)
new = date.toordinal(new)
date1 = current_date
list_interest = []
#print(current_balance)

getting Date months/ rest of days difference

is there a way to get these information from two dates:
calculate month between dates ( month for me is a complete month)
calculate rest of days between dates
here is my example:
start date:
01/01/2014
end date:
21/02/2014
i need a resualt like this : months:1 days:20
onother example:
start date:
15/01/2014
end date:
10/03/2014
i need a resualt like this : months:1 days:25
Using Java8 Date/Time API you may do it like so,
LocalDate startDate = LocalDate.of(2014, 1, 1);
LocalDate endDate = LocalDate.of(2014, 2, 21);
Period period = startDate.until(endDate);
System.out.println("months: " + period.getMonths() + " days: " + period.getDays());

Want to get previous date from current date

i have problem finding solution on getting previous date from current's date. but i only want the months to change. for example, now is August, i want the output to display 3 months ago = May.
Heres my code that i wrote
givenDate= "14-August-15"
DD = Day (givenDate)
'MsgBox DD
MM = Month (givenDate)
'MsgBox MM
YY = Year (givenDate)
'MsgBox YY
SysDate = DD&"/"&MM&"/"&YY
MsgBox Month(DateAdd("m", -3, "14-August-2015"))
'MsgBox(FormatDateTime(SysDate,1))
If you just need to display the name of the month that was 3 months ago, combine DateAdd(), Month(), and MonthName(). For example:
Dim dt1, dt2
dt1 = Date() ' Use today's date, for example
dt2 = DateAdd("m", -3, dt1) ' Subtract 3 months
WScript.Echo MonthName(Month(dt2)) ' Display the name of the month
Month() returns the month number (1 - 12). MonthName() takes that number and returns the name of the month ("January" - "December").

vbscript asp - Find every Thursday in a given month

I'm trying to find all Thursdays within a given Month using VBScript.
Can anyone help?
Here is one way;
base_date = cdate("21 aug 2011")
'get 1st thursday;
thurs = dateserial(year(base_date), month(base_date), 1)
if (weekday(thurs) <> 1) then thurs = 5 - weekday(thurs) + thurs
'loop subsequent;
do until month(thurs) <> month(base_date)
msgbox thurs
thurs = dateadd("d", 7, thurs)
loop
While the accepted answer does the job it's overly complicated for something that can be accomplished with WeekDay() function and a For loop.
Dim day
Dim startdate: startdate = CDate("21 aug 2011")
Dim enddate
'Get first day of month.
startdate = DateSerial(Year(startdate), Month(startdate), 1)
'Get last day of month.
enddate = DateAdd("m", 1, startdate) - 1
For day = startdate To enddate
If WeekDay(day) = vbThursday Then WScript.Echo day & " = " & WeekDayName(WeekDay(day))
Next
Output:
04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday
Any Date and Time constant can be used here to look for different or multiple weekdays with a little tinkering.