loader

For this tutorial in video form, click here

Let’s take a look at dates and times in Deluge Script, and how to perform functions upon them. The two most important time related functions in Deluge are toDate and toDateTime. You can follow along with the examples below by opening the Deluge interactive docs.

toDate

Let’s take a look at the “toDate” built-in function in Deluge:

First, you’ll notice the red string that represents a date. Due to a quirk of Deluge, we can add a line like this:

dateString = "1-Jan-2015";
info dateString.addDay(2);

Even though it’s not actually a date object yet, Deluge will interpret it as one, because it’s in its preferred date format. However, it’s best to always use toDate rather than assuming that Deluge will treat a string as a date. Otherwise you’ll get unexpected results as per below.

To make sure your dates get parsed correctly, you can specify the date format when you call toDate:

date_string = "1/1/2015";
converted_date = date_string.toDate("dd/MM/yyyy");

Refer to this docs page to see some more examples of date formats.

toDateTime

This function is pretty much the same deal but it can handle hours, minutes and seconds as well as the date.

For example:

time_string = "1/1/2015 20:30:54";
parsed_time = time_string.toDateTime("dd/MM/yyyy HH:mm:ss");

Refer to this docs page to find the available date time components:

Other Functions

Now that we’ve turned our string into a date, we can look at a variety of other neat functions. Check out one called addMonth:

In this example, we’ve added 15 months from January 1990 to get April 1991. There are functions for all types of dates, from addWeek to addYear.

Another cool function is getDay, which gives you the day of the month:

Other functions, like getMonth work in a similar fashion. You can also do this for day of week, day of year, week of year, and more!

For all those add functions, there are also sub functions to subtract time. Check out this example of subDay:

addBusinessDay is another neat one, but it assumes that your business works Monday to Friday. If your business has different operating hours, you can use the workday function which lets you specify which days are working days and which aren’t:

Another useful set of functions are daysBetween/weeksBetween/monthsBetween. Check out an example of monthsBetween below:

Notice how it gives you a whole number regardless of the exact amount of months. More precise measurement, like daysBetween can be useful here.

There are a lot of other miscellaneous functions available. Check out the docs for more details.

Conclusion

There are a lot of different time related functions in Deluge. Try as many as you can, and see what works for you. Good luck!

For this tutorial in video form, click here