[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Code to run every other day

I need to write some code to switch between two different videos on our website.  My boss would prefer doing a Monday/Wednesday/Friday arrangement one week, and then Tuesday/Thursday the next week.  What is the best way to automate this?
0
centdevs
Asked:
centdevs
  • 3
  • 3
1 Solution
 
jorgesv13Commented:
Use this code:
int week = DatePart(DateInterval.WeekOfYear, System.DateTime.Today);
    DayOfWeek d = Today.DayOfWeek;
    if (week % 2) {
        if (d == DayOfWeek.Monday | d == DayOfWeek.Wednesday | d == DayOfWeek.Friday) {
        }
        //Do Job
    }
    else {
        if (d == DayOfWeek.Tuesday | d == DayOfWeek.Thursday) {
        }
        //Do Job
    }

Open in new window

0
 
centdevsAuthor Commented:
This looks pretty close, there's still one other piece that I'm not sure how to handle in here though.  I can set the file name in the if blocks, and this code would work fine then for one week.  The next week though, the schedule reverses, showing the previous T/TH video on MWF, and vice versa.  Any ideas on how to handle that piece?
0
 
jorgesv13Commented:
Say you need to switch the 2 videos "myfirstvideo.avi" and "mysecondvideo.avi".
You should use something like this:
    string video1 = "";
    string video2 = "";
    int week = DatePart(DateInterval.WeekOfYear, System.DateTime.Today);
    DayOfWeek d = Today.DayOfWeek;
    if (week % 2) {
        if (d == DayOfWeek.Monday | d == DayOfWeek.Wednesday | d == DayOfWeek.Friday) {
            video1 = "myfirstvideo.avi";
            video2 = "mysecondvideo.avi";
        }
        else {
            video2 = "myfirstvideo.avi";
            video1 = "mysecondvideo.avi";
        }
    }
    else {
        if (d == DayOfWeek.Tuesday | d == DayOfWeek.Thursday) {
            video1 = "myfirstvideo.avi";
            video2 = "mysecondvideo.avi";
        }
        else {
            video2 = "myfirstvideo.avi";
            video1 = "mysecondvideo.avi";
        }
    }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
centdevsAuthor Commented:
Is DatePart VB only?  I'm getting a build error on DatePart.
0
 
jorgesv13Commented:
Yes, in c# you can use a function like this to get the week of the year:
public int GetYearWeek(DateTime time, string cultureInfo)
{
CultureInfo cul = new CultureInfo(cultureInfo);
return time.Year * 100 + cul.Calendar.GetWeekOfYear(time, cul.DateTimeFormat.CalendarWeekRule, cul.DateTimeFormat.FirstDayOfWeek);
}

Open in new window

0
 
centdevsAuthor Commented:
Wonderful, thank you!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now