SQL dates range conumdrum - vbscript

I have a table date range that i'm retrieving with one date specified x_VisitDate
And four variables defined that will use the x_VisitDate
For this instance x_VisitDate = 22 May 2014 the download button should only be available 10 days prior to the given date as at today we are 7 days away.
x_TodaysDate
x_DaysBeForeToDownloadFile = 10
x_AdditionalDayToDownloadFile = 1
x_CanDownloadFile = DateAdd("d", x_DaysBeForeToDownloadFile , x_VisitDate)
x_ExtraCanDownloadFile = DateAdd("d", x_AdditionalDayToDownloadFile, x_VisitDate)
I'm currently writing the following
If x_CanDownloadFile <= x_TodaysDate then
and it's showing the file when the date is set for 21 Oct 2018 (Future) and 23 Feb 2014 (past)
I'm going crazy here...
thanks

Don't use comparisons to constructed limits, but DateDiff for range checks. It's much easier to get right. Demo code:
>> nDaysAvailable = 3
>> dtToday = Date
>> For i = -5 To 0
>> dtCheck = DateAdd("d", i, dtToday)
>> nDiff = DateDiff("d", dtCheck, dtToday)
>> WScript.Echo dtCheck, nDiff, CStr(nDiff < nDaysAvailable)
>> Next
>>
10.05.2014 5 False
11.05.2014 4 False
12.05.2014 3 False
13.05.2014 2 True
14.05.2014 1 True
15.05.2014 0 True
(german locale)

Maybe I'm confused by your question but can't you just do this?
x_VisitDate = #5/22/14#
If Date >= x_VisitDate - 10 And Date <= x_VisitDate + 1 Then
' Show file
End If
Also, you don' t need to use DateDiff() when you're working with days. Just use integer math.

Related

console output of the current calendar month in Ruby

I need to output to the console the calendar of the current month in Ruby. The result should be similar to ncal on UNIX-like systems. I found a solution for C ++ but can't adapt for Ruby. So far, I only realized that I need to use nested loops to output the height and width. Tell me in which direction to move?
require 'date'
days = %w[Mun Tue Wed Thu Fri Sat Sun]
puts " #{Date::MONTHNAMES[Date.today.month]} #{Date.today.year}"
i = 0
start_month = (Date.today - Date.today.mday + 1).strftime("%a")
while i < days.size
print days[i]
j = 1
while j <= 31
if days[i] == start_month
print " #{j}"
end
j += 7
end
i += 1
puts
end
I'll take your solution so far, and try to give some specific pointers for how to progress with it - but of course, there are many different ways to approach this problem in general, so this is by no means the only approach!
The first critical issue (as you're aware!) is that you're only printing things for the row starting on the 1st of the month, due to this line:
if days[i] == start_month
Sticking with the current overall design, we know we'll need to print something for every line, so clearly a conditional like this isn't going to work. Let's try removing it.
Firstly, it will be more convenient to know which day of the week the month started on as a number, not a string, so we can easily calculate offsets against another day. Let's do that with:
# e.g. for 1st July 2021 this was a Thursday, so we get `4`.
start_of_month_weekday = (Date.today - Date.today.mday + 1).cwday
Next (and this is the crucial step!), we can use the above information to find out "which day of the month is it, on this day of the week?"
Here a first version of that calculation, incorporated into your solution so far:
require 'date'
days = %w[Mon Tue Wed Thu Fri Sat Sun]
puts " #{Date::MONTHNAMES[Date.today.month]} #{Date.today.year}"
i = 0
# e.g. for 1st July 2021 this was a Thursday, so we get `4`.
start_of_month_weekday = (Date.today - Date.today.mday + 1).cwday
while i < days.size
print days[i]
day_of_month = i - start_of_month_weekday + 2 # !!!
while day_of_month <= 31
print " #{day_of_month}"
day_of_month += 7
end
i += 1
puts
end
This outputs:
July 2021
Mon -2 5 12 19 26
Tue -1 6 13 20 27
Wed 0 7 14 21 28
Thu 1 8 15 22 29
Fri 2 9 16 23 30
Sat 3 10 17 24 31
Sun 4 11 18 25
Not bad! Now we're getting somewhere!
I'll leave you to figure out the rest 😉 .... But here are some clues, for what I'd tackle next:
This code, print " #{day_of_month}", needs to print a "blank space" if the day number is less than 1. This could be done with a simple if statement.
Similarly, since you want this calendar to line up neatly in a grid, you need this code to always print a something two characters wide. sprintf is your friend here! Check out the "Examples of width", about halfway down the page.
You've hardcoded 31 for the number of days in the month. This should be fixed, of course. (Use the Date library!)
It's funny how you used strftime("%a") in one place, yet constructed the calendar title awkwardly in the line above! 😄 Take a look at the documentation for formatting dates; it's extremely flexible. I think you can use: Date.today.strftime("%B %Y").
If you'd like to add some colour (or background colour?) to the current day of the month, consider doing something like this, or use a library to assist.
Using while loops works OK, but is quite un-rubyish. In 99% of cases, ruby has even better tools for the job; it's a very expressive language - iterators are king! (I'm guessing you first learned another language, before ruby? Seeing while loops, and/or for loops, is a dead giveaway that you're more familiar with a different language.) Instead of the outer while loop (while i < days.size), you could use days.each_with_index. And instead of the inner while loop (while j < 31), you could use day_of_month.step(31, 7) (how cool is that!!).
This is one way:
Construct a one-dimensional array, beginning with the daynames (Mon Tue ...).
Figure out a way to determine with how many "blanks" the month starts (these are days from the previous month. wday might help). Attach that amount of empty strings to the array.
Determine how many days the month has (hint Date.new(2021,7,-1), and attach all these daynumbers to the array.
Attach empty strings to the array until the size of the array is divisible by 7 (or better, calculate). Skip this if you're skipping the last bullet.
Convert all elements of this array to right-adjusted strings of size 3 or some-such.
Use each_slice(7) to slice the array into weeks.
If desired, transpose this array of week-slices to mimic the ncal output.
Thank you for your help, literally 10 hours and I figured it out thanks to you. I apologize once again for the initially incorrectly posed question.
With the help of hints, I assembled such a solution.
require 'date'
days = %w[Mon Tue Wed Thu Fri Sat Sun]
p days
blanks = Date.new(2021,7,1).wday - 1
blanks.times do
days.push(' ')
end
days_in_month = Date.new(2021, 7, -1).day
days_in_month
day = 1
while day <= days_in_month
days.push(day)
day += 1
end
unless (days.size % 7) == 0
days.push(' ')
end
days.join(', ')
new_arr = days.each_slice(7).to_a
puts"Массив дней: #{new_arr}"
for i in 0...7
for j in 0...new_arr.size
print " #{new_arr[j][i]}"
end
puts
end
require 'date'
# init
DAYS_ORDER = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
today = Date.today
month = today.month
year = today.year
first_day = Date.new(year, month, 1)
last_day = Date.new(year, month, -1)
hash_days = {}
# get all current months days and add to hash_days
first_day.upto(last_day) { |day| hash_days[day.day] = day.strftime('%a') }
# group by wday
grouped_hash = hash_days.group_by { |day| day.pop }.transform_values { |days| days.flatten }
# sort by wday from DAYS_ORDER
sorted_arr = grouped_hash.sort_by { |k, v| DAYS_ORDER.index(k) }
# rendering current month's calendar with mark current day
## title
print "\x1b[4m#{today.strftime("%B %Y")}\x1b[0m\n"
## calendar
indent = true
sorted_arr.each do |wday, days|
print wday
if days[0] != 1 && indent == true
print " "
else
indent = false
end
days.each do |value|
spaces = " " * (value > 9 ? 1 : 2)
str_day = spaces + value.to_s
current_day = "\x1b[1;31m#{str_day}\x1b[0m"
print value == today.day ? current_day : str_day
end
puts
end
view

How to create a proper calculation of a due date in a dynamic manner in laravel

I been analyzing the proper calculation of the duedate for my app. I am working with a lending app where I need to display the due date for the borrower.
let say the approved date of their loan is 2019-10-27 and today is 11-5-2019. the payment scheme is dynamic. depending on what the admin set. in this example, the payment scheme weekly so I just need to say;
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
from my example above the due date is supposedly on Nov 3, 2019. Now how can I make it say that the next due date is on Nov 10, 2019?
I really tried to analyze, here's my thought.
I get the difference between the approval date and NOW()
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
now I compared the $diff with $loan_application->scheme->num_days. so let say the $diff=9 and $loan_application->scheme->num_days = 7
now I created the condition
if ($diff > $loan_application->scheme->num_days) {
//should display
Nov 10, 2019
//because the current date is already Nov 5
}
and after Nov 10, + 7 days again, and + 7 days again and so on.
here is the complete code I already have;
$dateApproved = Carbon::parse($loan_application->date_approval)->toFormattedDateString();
$now = Carbon::today('M d Y');
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
$numOfScheme = $loan_application->loanDuration->num_days / $loan_application->scheme->num_days;
if ($diff > $loan_application->scheme->num_days) {
}
I hope you understand my question. Please help. Thank you so much in advance.
this will add number of days from payment scheme. in my example 7 days. but how can I tell my code that today is already 2 days late, add additional 7 days
It took some time but I understood your question.
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
intdiv is just php's integer division.

Number of days between two Time instances

How can I determine the number of days between two Time instances in Ruby?
> earlyTime = Time.at(123)
> laterTime = Time.now
> time_difference = laterTime - earlyTime
I'd like to determine the number of days in time_difference (I'm not worried about fractions of days. Rounding up or down is fine).
Difference of two times is in seconds. Divide it by number of seconds in 24 hours.
(t1 - t2).to_i / (24 * 60 * 60)
require 'date'
days_between = (Date.parse(laterTime.to_s) - Date.parse(earlyTime.to_s)).round
Edit ...or more simply...
require 'date'
(laterTime.to_date - earlyTime.to_date).round
earlyTime = Time.at(123)
laterTime = Time.now
time_difference = laterTime - earlyTime
time_difference_in_days = time_difference / 1.day # just divide by 1.day
[1] pry(main)> earlyTime = Time.at(123)
=> 1970-01-01 01:02:03 +0100
[2] pry(main)> laterTime = Time.now
=> 2014-04-15 11:13:40 +0200
[3] pry(main)> (laterTime.to_date - earlyTime.to_date).to_i
=> 16175
To account for DST (Daylight Saving Time), you'd have to count it by the days. Note that this assumes less than a day is counted as 1 (rounded up):
num = 0
cur = start_time
while cur < end_time
num += 1
cur = cur.advance(:days => 1)
end
return num
Here is a simple answer that works across DST:
numDays = ((laterTime - earlyTime)/(24.0*60*60)).round
60*60 is the number of seconds in an hour
24.0 is the number of hours in a day. It's a float because some days are a little more than 24 hours, some are less. So when we divide by the number of seconds in a day we still have a float, and round will round to the closest integer.
So if we go across DST, either way, we'll still round to the closest day. Even if you're in some weird timezone that changes more than an hour for DST.
in_days (Rails 6.1+)
Rails 6.1 introduces new ActiveSupport::Duration conversion methods like in_seconds, in_minutes, in_hours, in_days, in_weeks, in_months, and in_years.
As a result, now, your problem can be solved as:
date_1 = Time.parse('2020-10-18 00:00:00 UTC')
date_2 = Time.parse('2020-08-13 03:35:38 UTC')
(date_2 - date_1).seconds.in_days.to_i.abs
# => 65
Here is a link to the corresponding PR.
None of these answers will actually work if you don't want to estimate and you want to take into account daylight savings time.
For instance 10 AM on Wednesday before the fall change of clocks and 10 AM the Wednesday afterwards, the time between them would be 1 week and 1 hour. During the spring it would be 1 week minus 1 hour.
In order to get the accurate time you can use the following code
def self.days_between_two_dates later_time, early_time
days_between = (later_time.to_date-early_time.to_date).to_f
later_time_time_of_day_in_seconds = later_time.hour*3600+later_time.min*60+later_time.sec
earlier_time_time_of_day_in_seconds = early_time.hour*3600+early_time.min*60+early_time.sec
days_between + (later_time_time_of_day_in_seconds - early_time_time_of_day_in_seconds)/1.0.day
end

get decimal amount of months for date range

I try to get a decimal amount of months for a date range. Example:
ruby-1.9.2-p0 > from = Date.new(2011, 7, 6)
=> Wed, 06 Jul 2011
ruby-1.9.2-p0 > to = Date.new(2011, 8, 31)
=> Wed, 31 Aug 2011
ruby-1.9.2-p0 > to - from
=> (56/1)
So the difference is 56 days. But I want and need the amount of months: 1.83
I have created the following piece of code which returns the correct result but doesn't feel like the ruby way:
months = Hash.new
(from..to).each do |date|
unless months.key? date.beginning_of_month
months[date.beginning_of_month] = 1
else
months[date.beginning_of_month] += 1
end
end
multiplicator = 0.0
months.each do |month, days|
multiplicator += days.to_f/month.end_of_month.day
end
return multiplicator.floor_to(2)
To be honest: It looks ugly and really inefficient. But I just cannot figure out any easier way.
Can you help me to find a better solution?
For further questions feel free to ask me.
Many thanks in advance!
Update/Solution: Solved the problem with the following piece of code:
months = 0.0
months += ((date_to < date_from.end_of_month ? date_to : date_from.end_of_month) - date_from + 1) / Time.days_in_month(date_from.month)
unless date_to.month == date_from.month
months += (date_to - date_to.beginning_of_month + 1) / Time.days_in_month(date_to.month)
months += date_to.month - date_from.month - 1
end
return months.floor_to(2)
a better way to do would be summation of
number of days left in from / number of days in from
number of days completed in to / number of days in to
number of months between from and to (from, to excluded)
This way you wont have iterations to do

How do I get the Date & Time (VBS)

How do I get the current date and time using VBS (for Windows. I'm not looking for VBScript for ASP/ASPX or webpages).
Here's various date and time information you can pull in vbscript running under Windows Script Host (WSH):
Now = 2/29/2016 1:02:03 PM
Date = 2/29/2016
Time = 1:02:03 PM
Timer = 78826.31 ' seconds since midnight
FormatDateTime(Now) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbGeneralDate) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbLongDate) = Monday, February 29, 2016
FormatDateTime(Now, vbShortDate) = 2/29/2016
FormatDateTime(Now, vbLongTime) = 1:02:03 PM
FormatDateTime(Now, vbShortTime) = 13:02
Year(Now) = 2016
Month(Now) = 2
Day(Now) = 29
Hour(Now) = 13
Minute(Now) = 2
Second(Now) = 3
Year(Date) = 2016
Month(Date) = 2
Day(Date) = 29
Hour(Time) = 13
Minute(Time) = 2
Second(Time) = 3
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
LPad(Month(Date), "0", 2) = 02
LPad(Day(Date), "0", 2) = 29
LPad(Hour(Time), "0", 2) = 13
LPad(Minute(Time), "0", 2) = 02
LPad(Second(Time), "0", 2) = 03
Weekday(Now) = 2
WeekdayName(Weekday(Now), True) = Mon
WeekdayName(Weekday(Now), False) = Monday
WeekdayName(Weekday(Now)) = Monday
MonthName(Month(Now), True) = Feb
MonthName(Month(Now), False) = February
MonthName(Month(Now)) = February
Set os = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=#")
os.LocalDateTime = 20131204215346.562000-300
Left(os.LocalDateTime, 4) = 2013 ' year
Mid(os.LocalDateTime, 5, 2) = 12 ' month
Mid(os.LocalDateTime, 7, 2) = 04 ' day
Mid(os.LocalDateTime, 9, 2) = 21 ' hour
Mid(os.LocalDateTime, 11, 2) = 53 ' minute
Mid(os.LocalDateTime, 13, 2) = 46 ' second
Dim wmi : Set wmi = GetObject("winmgmts:root\cimv2")
Set timeZones = wmi.ExecQuery("SELECT Bias, Caption FROM Win32_TimeZone")
For Each tz In timeZones
tz.Bias = -300
tz.Caption = (UTC-05:00) Eastern Time (US & Canada)
Next
Source
To expound on Numenor's answer you can do something like, Format(Now(),"HH:mm:ss")
using these custom date/time formating options
For everyone who is tempted to downvote this answer please be aware that the question was originally tagged VB and vbscript hence my answer, the VB tag was edited out leaving only the vbscript tag. The OP accepted this answer which I take to mean that it gave him the information that he needed.
For VBScript use FormatDateTime, which has 5 numerical arguments to give you one of 5 predefined formats. Its not great.
FormatDateTime(now, 4)
08:12
This is an old question but alot of the answers in here use VB or VBA. The tag says vbscript (which is how I got here).
The answers here got kind of muddled since VB is super broad where you can have so many applications of it. My answer is solely on vbscript and accomplishes my case of formatting in YYYYMMDD in vbscript
Sharing what I've learned:
There are all the DateTime functions in vbscript defined here so you can mix-n-match to get the result that you want
What I needed was to get the current date and format it in YYYYMMDD to do that I just needed to concat DatePart like so for the current Date: date = DatePart("yyyy",Date) & DatePart("m",Date) & DatePart("d",Date)
That's all, I hope this helps someone.
Show time in form 24 hours
Right("0" & hour(now),2) & ":" & Right("0" & minute(now),2) = 01:35
Right("0" & hour(now),2) = 01
Right("0" & minute(now),2) = 35
nowreturns the current date and time
There are also separate Time() and Date() functions.

Resources