Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


amber.rv
2020-05-28T03:48:28Z
Hi, I've used this to get the last week's work week
{MATH(Subtract|Integer|{DATE(Week)}|1|#0)}
How can I output the work week into 2 digit? Is it by adding another 0? E.g.
{MATH(Subtract|Integer|{DATE(Week)}|1|#00)}

The above code also does not work for first week of the year. If I use the .net code provided by support:
using System;
using System.Globalization;

public class Test
{
public static string GetPreviousWeek()
{
// Gets the Calendar instance associated with a CultureInfo.
CultureInfo myCI = CultureInfo.CurrentCulture;
Calendar myCal = myCI.Calendar;

// Gets the DTFI properties required by GetWeekOfYear.
CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

return myCal.GetWeekOfYear(DateTime.Now.Subtract(new TimeSpan(7,0,0,0,0)), myCWR, myFirstDOW).ToString();
}
}

How can I make the output for single digit weeks to double digit?

Thank you.
Sponsor
Forum information
Gary_W
2020-05-28T15:09:07Z
This powershell code should do the trick.

$week=get-date (get-date).adddays(-7) -UFormat "%V"
if ([decimal]$week -lt 10){
  '0' + $week
}
else{
  $week
}


Use the inner get-date to get the current date then subtract 7 days. This returns a string so you need the outer get-date to turn it back into a date datatype in order to use the Uformat argument %V which returns the week of the year as a string. Then a simple test to see if a leading 0 is needed.

Note you could supply a date argument to the inner get-date too.

get-date (get-date "1/4/2020").adddays(-7) -UFormat "%V"


The return is 52 proving it handles wrapping back around the year.
amber.rv
2020-05-29T04:01:44Z
Originally Posted by: Gary_W 

This powershell code should do the trick.

$week=get-date (get-date).adddays(-7) -UFormat "%V"
if ([decimal]$week -lt 10){
  '0' + $week
}
else{
  $week
}


Use the inner get-date to get the current date then subtract 7 days. This returns a string so you need the outer get-date to turn it back into a date datatype in order to use the Uformat argument %V which returns the week of the year as a string. Then a simple test to see if a leading 0 is needed.

Note you could supply a date argument to the inner get-date too.

get-date (get-date "1/4/2020").adddays(-7) -UFormat "%V"


The return is 52 proving it handles wrapping back around the year.



Thank you. When I tried to test it for Jan 7 2020 - instead of giving me 52, it returned 53. Same when I enter Jan 6 2020. However, Jan 5 and Jan 8 gave the right output. Any idea why? How to fix?
Gary_W
2020-05-29T13:25:21Z
Actually that's correct as if you Google "how many weeks in 2019" you'll see there are 52.142857142857 weeks in 2019. Interesting, eh? From www.convertunits.com :
"There are exactly 52.142857142857 weeks in the year 2019. This is equivalent to 52 weeks and 1 extra day, since there are 365 total days in 2019. Most years have 365 days, but a leap year has 366 days. That adds up to 52 weeks (where each week is exactly 7 days) PLUS 1 or 2 additional days."

I suspect if you wish to stick to the idea that every year has 52 weeks you'll need a test that allows for that right after setting $week on the first line:

# Allow for leap years
if ($week -eq 53){
  $week=52
}


I commend you on your thorough testing!

Gary
amber.rv
2020-05-29T14:23:10Z
Hi Gary,

Thank you again. I'm a QA therefore a bit thorough in testing 😉.

This resolved the problem for 2019. However, does not work for next year when 2020 has 53 weeks. Help again? 🙂 Thank you.
Gary_W
2020-05-29T14:32:35Z
The edit allows for that (maybe you didn't see the edit to my last post), but if a year *can* have 53 weeks, maybe your business rules need to not fight it and deal with it? An interesting conundrum. Let us know how you resolve it. I'm interested to know why you want to force 52 weeks in a 53 week year anyway. I'm not picking a fight, just curious why. 🙂
amber.rv
2020-05-29T14:50:26Z
Hi Gary,

I'm using it to grab a data with the corresponding week number in the filename. Looking at work week at Epoch Converter site, 2019 only has 52 and 2020 has 53.

The 2 codes in my original post works but both with possible issues. The .net code seems to be a better solution however, not sure if it'll show double digit for work week 1-9 (perhaps not) and I am not sure how to make that happen. I am hoping someone (or Henrik) is able to help (I got the code from his post).

Appreciate the help!

Cheers!
ErikC
2020-06-09T07:25:15Z
Hi,

In your C# code,
1) you can change the .ToString(); to .ToString("00");
2) Or you can do add .PadLeft(2,'0'); to the end of the ToString()

what it does:
1) you are telling to write always two numbers
2) the string must be 2 long, if not add '0's to the left of it

Choose your solution, both are okay, but choose one (or nr two, lol).

Regards,
Erik
Uses Visualcron since 2006.
amber.rv
2020-06-15T01:50:36Z
Originally Posted by: ErikC 

Hi,

In your C# code,
1) you can change the .ToString(); to .ToString("00");
2) Or you can do add .PadLeft(2,'0'); to the end of the ToString()

what it does:
1) you are telling to write always two numbers
2) the string must be 2 long, if not add '0's to the left of it

Choose your solution, both are okay, but choose one (or nr two, lol).

Regards,
Erik



Hi Erik - I'll try that. Thank you.
Scroll to Top