Converting date to string in vb script - vbscript

I have date like 6/24/2013 , i want to get only month name and date like June 24th as output in vb script.

Use two functions to deal with the two (sub) problems - name of month, ordinal of number - separately:
Option Explicit
Dim n
For n = -2 To 10
WScript.Echo fmtDate(DateAdd("d", n, Date))
Next
Function fmtDate(dtX)
fmtDate = MonthName(Month(dtX)) & " " & ordinal(Day(dtX))
End Function
' !! http://stackoverflow.com/a/4011232/603855
Function ordinal(n)
Select Case n Mod 10
case 1 : ordinal = "st"
case 2 : ordinal = "nd"
case 3 : ordinal = "rd"
case Else : ordinal = "th"
End Select
ordinal = n & ordinal
End Function
output:
June 22nd
June 23rd
June 24th
June 25th
June 26th
June 27th
June 28th
June 29th
June 30th
July 1st
July 2nd
July 3rd
July 4th
Update:
(Hopefully) improved version of ordinal():
Function ordinal(n)
Select Case n Mod 31
case 1, 21, 31 : ordinal = "st"
case 2, 22 : ordinal = "nd"
case 3, 23 : ordinal = "rd"
case Else : ordinal = "th"
End Select
ordinal = n & ordinal
End Function

Related

Trying to Sync weekdays between 2 years

I'm trying to come up with something to Sync weekdays between years
So far I can do it for a 4 year gap just by subtracting 364 days
For example
Monday Feb 1 2021 - 364 days becomes Monday Feb 3rd 2020
Friday Feb 19 2021 - 364 days becomes Friday Feb 21nd 2020
Tuesday Mar 2 2021 - 364 days becomes Tuesday Mar 3rd 2020
notice how the weekday is in perfect sync (Monday to Monday, Tuesday to Tuesday etc)
and I can do this for 2 years just by using 728 days (364 * 2)
and so on for 3 and 4 years
my problem is after 4 years it stops working
if I do the same thing for 5 Years (364*5)
Monday Feb 1st 2021 becomes Monday Feb 8th 2016
however I would want it to be Monday Feb 1st 2016
I cant seem to crack how to deal with this for 5 years on
This is using Zeller’s Rule to get the day number, and then just modifying the actual date when subtracting 365 days to get the day name to match. I'm sure there are edge cases (e.g. I believe this would currently allow a result of something like "March 33"), but this should get you started:
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const dayNum = (day, month, year) => {
if (month <= 2) {
year--;
month += 12;
}
month-=2;
const lastTwoDigitsOfYear = year % 100;
const firstTwoDigitsOfYear = (year - lastTwoDigitsOfYear) / 100
let F = day + Math.floor((13*month-1)/5) + lastTwoDigitsOfYear + Math.floor(lastTwoDigitsOfYear/4) +Math.floor(firstTwoDigitsOfYear/4)-2*firstTwoDigitsOfYear
let f = F >= 0 ? (F % 7) : 7 + (F % 7);
return f
}
// Ok, so we can get the day for any date.
const syncedYearAdd = (day, month, year, numYears) => {
const d1 = dayNum(day, month, year);
const d2 = dayNum(day, month, year + numYears);
if (d1 < d2) {
day -= (d2 - d1)
} else if (d1 > d2) {
day += (d1 - d2);
}
if (day < 0) {
day += 7;
}
console.log(days[d1], day, months[month-1], year + numYears)
}
// Monday Feb 1st 2021
for (let i = 0; i <= 5; i++) {
syncedYearAdd(1, 2, 2021, i * -1)
}

Ruby - Get time at start of next minute

I'm looking for a concise way to get a Ruby Time object representing the top of the next minute (and hour/day/month/year, if possible). I want this to work in a pure Ruby environment, so the Rails function Time.change or similar doesn't fit the bill.
At first this seems simple - just add 1 to Time.now, but there are edge cases where if, for example, you try to instantiate a Time object with Time.now.min + 1 when the current minute is 59, you get an ArgumentError: min out of range. This goes for hour, day, and month as well.
I have some lengthy code that does the job. It's ugly, but I'm just experimenting:
def add_minute
now = Time.local
year = now.year
month = now.month
day = now.day
hour = now.hour
min = now.min
if now.min == 59
if now.hour == 23
if now.day == Date.civil(now.year, now.month, -1).day
if month == 12
year = year + 1
month = 1
day = 1
hour = 0
min = 0
else
month = now.month + 1
day = 1
hour = 0
min = 0
end
else
day = now.day + 1
hour = 0
min = 0
end
else
hour = now.hour + 1
min = 0
end
else
min = now.min + 1
end
Time.local year, month, day, hour, min, 0
end
This seems absurdly verbose for what seems like it should be a simple or built-in task, but I haven't found a native Ruby solution. Does one exist?
You could convert the Time object to UNIX epoch time (seconds since 1970) using #to_i, add 60 s, and then convert back to a Time object.
time_unix = Time.now.to_i
time_unix_one_min_later = time_unix + 60
time_one_min_later = t = Time.at(time_unix_one_min_later)
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT: Even shorter - you can just add integer seconds to Time.now directly:
time_one_min_later = t = Time.now + 60
time_one_min_later_rounded_down = Time.new(t.year, t.month, t.day, t.hour, t.min)
EDIT 2: One-liner - just subtract Time.now.sec:
time_one_min_later_rounded_down = Time.now + 60 - Time.now.sec
Other option, given one second to midnight:
require 'time'
now = Time.strptime('2018-12-31 23:59:59', '%Y-%m-%d %H:%M:%S')
Within one minute:
Time.at(now + 60) #=> 2019-01-01 00:00:59 +0100
Time.at(now + 60 - now.sec) #=> 2019-01-01 00:00:00 +0100
You get: # HAPPY NEW YEAR!
Ruby has built in methods for adding months (>>) and days (+). A year is 12 months, and an hour is 1/24th of a day.
require 'date'
def add_time(time, year: 0 ,month: 0, day: 0, hour: 0, minute: 0)
time >>= 12*year
time >>= month
time += day
time += Rational(hour,24) # or (hour/24.0) if you dislike rationals
time += Rational(minute, 24*60) # (minute/24.0*60) if you dislike rationals
end
p t = DateTime.now
p add_time(t, year: 1, minute: 30)
Not that clean without ActiveSupport:
new_date = (DateTime.now + 1.to_f / (60*24))
DateTime.new(new_date.year, new_date.month, new_date.day, new_date.hour, new_date.minute)
We can make this calculation easier to understand by getting the current number of seconds we are through the day. (optional)
DateTime.current.to_i gives us the number of seconds since 1970
DateTime.current.to_i - DateTime.current.beginning_of_day.to_i gives us the number of seconds since the start of the day.
(((number_of_seconds_through_the_day + 60)/60) * 60) gives us the number of seconds we will be at when the next minute starts
Then we subtract the two to give us the number of seconds until the top of the next minute.
If we want the exact time at start of the next minute then we can do:
DateTime.current + seconds_until_start_of_the_next_minute.seconds
def seconds_until_start_of_the_next_minute
number_of_seconds_through_the_day = DateTime.current.to_i - DateTime.current.beginning_of_day.to_i
number_of_seconds_through_the_day_at_next_minute = (((number_of_seconds_through_the_day + 60)/60) * 60)
seconds_until_next_minute_starts = number_of_seconds_through_the_day_at_next_minute - number_of_seconds_through_the_day
return seconds_until_next_minute_starts
end

Comparing variables to multiple values and using "and" & "or in "If" condition vbscript

I am learning qtp and vbs mostly by myself. I need a function in qtp/vbs server 2008 that ads one day to current date then changes the places of dd and mm and transforms it to a numeric string for qtp to insert it to application under test. This is what I came up with, but the function gives back nothing at the end.
'5) DATEFORMATFUNC
'Function that gets current system date, adds one day to it and transforms it to a string
'usable as a date parameter in UAT "Flight Reservation"
Function DATEFORMATFUNC
'Defining variables
Dim currentday, currentmonth, currentyear, oday, omonth, oyear, NextDayDate, LeapYear
'* Checking if current year is leap year and transforming answer to numeric variant wher 1=True and 0=False
If DatePart("yyyy", Now) mod 4=0 then
LeapYear = 1
Else LeapYear = 0
End If
'* getting current day of month
currentday = DatePart("d", Now)
'* getting current month of year
currentmonth = DatePart("m", Now)
'* reporting to get it into "test results"
Reporter.ReportEvent micDone, "Current month of year is " & currentmonth, currentmonth
'* getting last two numbers of current year
currentyear = Right((DatePart("yyyy", Now)),2)
'* reporting to get it into "test results"
Reporter.ReportEvent micDone, "Current year is " & currentyear, currentyear
'* If currentday is 31(when 31 days in month) then date becomes the first day of next month
If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then
omonth = currentmonth +1 and oday = 1
'* If currentday is less then 31(when 31 days in month) then adding 1 day to currentday
ElseIf currentmonth = 1 or 3 or 5 or 7 or 8 or 10 or 12 and currentday < 31 Then
oday = currentday + 1
'* if current day is 31 of december then date becomes first of january next year
ElseIf currentmonth = 12 and currentday = 31 Then
omonth = 1 and oday = 1 and oyear = currentyear+1
'* if current day is 30 (when 30 days in month) then date becomes first day of next month
ElseIf omonth = 4 or 6 or 9 or 11 and oday = 30 Then
omonth =currentmonth + 1 and oday = 1
'* if current day is less then 30 (when 30 days in month) then adding 1 day to current day
ElseIf currentmonth = 4 or 6 or 9 or 11 and currentday < 30 Then
oday = currentday +1
'* if it is leap year and current day is 28 of February then adding 1 day to current day
ElseIf currentmonth = 2 and currentday< 29 and LeapYear = 1 Then
oday = currentday +1
'* if it is leap year and date is 29 of February then date jumps to first of March
ElseIf currentmonth = 2 and currentday = 29 and LeapYear = 1 Then
oday = 1 and omonth = 3
'* if it is not leap year and current day is 28 of February then date becomes first of March
ElseIf currentmonth = 2 and currentday = 28 and LeapYear = 0 Then
oday = 1 and omonth = currentmonth + 1
'* if it is not leap year and current day is less then 28 then adding 1 day to current day
ElseIf currentmonth = 2 and currentday < 28 and LeapYear = 0 Then
oday = currentday + 1
'* End
End If
'if the day of the month is a one digit number then concatinating "0" before it
If oday < 10 then
oday = 0 & oday
end If
'if the month of the year is a one digit number then concatenating "0" before it
If omonth < 10 then
omonth = 0 & omonth
End If
'concatinating the parts of the date in a manner that it can be used as a date parameter (mmddyy) for "Flight Reservation"
NextDayDate = omonth & oday & oyear
Reporter.ReportEvent micDone, "Current date vs changed date", "Current date is: " & Date & " Changed date is: " & NextDayDate
'reporting to get it into "test results"
Reporter.ReportEvent micDone, "The parameter inserted to Data Table " , DataTable.Value ("Next_Day_Date", dtGlobalSheet)
'inserting parameter to data table
DataTable.Value ("Next_Day_Date", dtGlobalSheet) = NextDayDate
End Function
Conditions like your
If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then
don't 'work', because VBScript's logical operators do bitwise operations on numbers (cf. Or, Lippert):
>> For i = 0 To 1
>> If i = 1 Or i = 2 Then
>> WScript.Echo i, "is 1 Or 2"
>> End If
>> Next
>>
1 is 1 Or 2
For i = 0 the condition evaluates to False Or False i.e. False. But in
>> For i = 0 To 1
>> If i = 1 Or 2 Then
>> WScript.Echo i, "is 1 Or 2"
>> End If
>> Next
>>
0 is 1 Or 2
1 is 1 Or 2
the False of i = 0 is taken as the number 0 and bitwise Or'ed to 2 which is 2 and therefore True.
Before you add lots of currentmonth = to your conditionals, consider DateAdd():
>> dtToday = Date()
>> WScript.Echo TypeName(dtToday), dtToday
>> dtTomorrow = DateAdd("d", 1, dtToday)
>> WScript.Echo TypeName(dtTomorrow), dtTomorrow
>>
Date 03.05.2015 ' german locale
Date 04.05.2015
>> For Each dtX In Array(#12/31/2014#, #2/28/2012#, #2/28/2013#)
>> WScript.Echo dtX, DateAdd("d", 1, dtX)
>> Next
>>
31.12.2014 01.01.2015 ' german locale
28.02.2012 29.02.2012
28.02.2013 01.03.2013
And to format a Date as MMDDYYYY:
>> dtX = Date()
>> sX = Right(100 + Month(dtx), 2) & Right(100 + Day(dtX), 2) & Year(dtX)
>> WScript.Echo TypeName(sX), sX
>>
String 05032015

One line if statement in Ruby

I have following piece of code:
if day > 31
day -= 31
month = "April"
end
Can I write it in one line different than:
if day > 31 then day -= 31 and month = "April" end
?
I've tried it like:
if day > 31 {day -= 31; month = "April"}
But it doesn't work
(day -= 31; month = "April") if day > 31
Alternate way (As suggested by #mudasobwa in comments below) :
day, month = day - 31, "April" if day > 31

Ruby Determine Season (Fall, Winter, Spring or Summer)

I am working on a script that is supposed to determine the "season" of the year based on date ranges:
For Example:
January 1 - April 1: Winter
April 2 - June 30: Spring
July 1 - September 31: Summer
October 1 - December 31: Fall
I am not sure how the best way (or the best ruby way) to go about doing this. Anyone else run across how to do this?
31 September?
As leifg suggested, here it is in code:
require 'Date'
class Date
def season
# Not sure if there's a neater expression. yday is out due to leap years
day_hash = month * 100 + mday
case day_hash
when 101..401 then :winter
when 402..630 then :spring
when 701..930 then :summer
when 1001..1231 then :fall
end
end
end
Once defined, call it e.g. like this:
d = Date.today
d.season
You could try with ranges and Date objects:
http://www.tutorialspoint.com/ruby/ruby_ranges.htm
without ranges.
require 'date'
def season
year_day = Date.today.yday().to_i
year = Date.today.year.to_i
is_leap_year = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
if is_leap_year and year_day > 60
# if is leap year and date > 28 february
year_day = year_day - 1
end
if year_day >= 355 or year_day < 81
result = :winter
elsif year_day >= 81 and year_day < 173
result = :spring
elsif year_day >= 173 and year_day < 266
result = :summer
elsif year_day >= 266 and year_day < 355
result = :autumn
end
return result
end
Neil Slater's answer's approach is great but for me those dates aren't quite correct. They show fall ending on December 31st which isn't the case in any scenario I can think of.
Using the northern meteorological seasons:
Spring runs from March 1 to May 31;
Summer runs from June 1 to August 31;
Fall (autumn) runs from September 1 to November 30; and
Winter runs from December 1 to February 28 (February 29 in a leap year).
The code would need to be updated to:
require "date"
class Date
def season
day_hash = month * 100 + mday
case day_hash
when 101..300 then :winter
when 301..531 then :spring
when 601..831 then :summer
when 901..1130 then :fall
when 1201..1231 then :winter
end
end
end

Resources