Inputting a specific date into ParseHub is just like inputting any other text string (ex. Email address, password, etc.). However, ParseHub has built-in functions to input dates in relation to the current date (ex. today's date, yesterday's date, date one month from now, etc.). In this tutorial, we are going to show you how to use these functions to get a variety of different dates.
$date.toString() function
You can get today's date on ParseHub using the $date.toString() method. Between the parenthesis you can add in how you would like your date formatted using the table below for standard date and time format specifiers:
Format | Description | Example |
---|---|---|
s | The seconds of the minute between 0-59. | "0" to "59" |
ss | The seconds of the minute with leading zero if required. | "00" to "59" |
m | The minute of the hour between 0-59. | "0" or "59" |
mm | The minute of the hour with leading zero if required. | "00" or "59" |
h | The hour of the day between 1-12. | "1" to "12" |
hh | The hour of the day with leading zero if required. | "01" to "12" |
H | The hour of the day between 0-23. | "0" to "23" |
HH | The hour of the day with leading zero if required. | "00" to "23" |
d | The day of the month between 1 and 31. | "1" to "31" |
dd | The day of the month with leading zero if required. | "01" to "31" |
ddd | Abbreviated day name. Date.!CultureInfo.abbreviatedDayNames. | "Mon" to "Sun" |
dddd | The full day name. Date.!CultureInfo.dayNames. | "Monday" to "Sunday" |
M | The month of the year between 1-12. | "1" to "12" |
MM | The month of the year with leading zero if required. | "01" to "12" |
MMM | Abbreviated month name. Date.!CultureInfo.abbreviatedMonthNames. | "Jan" to "Dec" |
MMMM | The full month name. Date.!CultureInfo.monthNames. | "January" to "December" |
yy | Displays the year as a two-digit number. | "99" or "07" |
yyyy | Displays the full four digit year. | "1999" or "2007" |
t | Displays the first character of the A.M./P.M. designator. Date.!CultureInfo.amDesignator or Date.!CultureInfo.pmDesignator | "A" or "P" |
tt | Displays the A.M./P.M. designator. Date.!CultureInfo.amDesignator or Date.!CultureInfo.pmDesignator | "AM" or "PM" |
S | The ordinal suffix ("st, "nd", "rd" or "th") of the current day. | "st, "nd", "rd" or "th" |
So, for example, these would be the codes for the following dates:
- "Wednesday, 8 November, 2017" - $date.toString("dddd, d MMMM, yyyy")
- "November 8, 2017" - $date.toString("MMMM d, yyyy")
- "08-11-2017" - $date.toString("dd-MM-yyyy")
- "Nov 8, '17" - $date.toString("MMM d, 'yy")
You can also add and subtract days, months and years from the current date. You can do this using the addDays(), addMonths(), and addYears() parameters.
Taking the date "November 8, 2017" from above as an example:
- "November 13, 2017" - $date.addDays(5).toString("MMMM d, yyyy")
- "September 8, 2017" - $date.addMonths(-2).toString("MMMM d, yyyy")
- "November 8, 2025" - $date.addYears(8).toString("MMMM d, yyyy")
- "September 13, 2025" - $date.addDays(5).addMonths(-2).addYears(8).toString("MMMM d, yyyy")
Example: Travelocity.ca
This example will show you how to enter a specific date range on a website in order to scrape updated results on a regular basis. This will involve using inputting the $date.toString() function to evaluate today's date, or a date in the future.
To follow along you can start a project for this URL: "https://www.travelocity.ca"
1. Click on the "Select page" command + button that is located on the right of the command. From the toolbox that appears, choose the "Select" tool.
2. Click on the "Flying From" input field to Select it.
3. An "Input" command will be created automatically. Enter the name of the target City:
4. Click on the "Select page" command + button that is located on the right of the command. From the toolbox that appears, choose the "Select" tool.
5. Click on the "Flying To" input field to Select it.
6. An "Input" command will be created automatically. Enter the name of the target City:
7. Click on the "Select page" command + button that is located to the right of the command. From the toolbox that appears, choose the "Select" tool.
8. Click on the "Departing" date field.
9. An "Input" command will be created automatically. Let's assume that we want to enter Today's date and schedule the project to run on a daily basis. This means that every day, the project will be run with the date evaluating to that day's date. To be able to achieve this goal, you should enter the date in expression format to be able to scrape the updated results without needing to modify the project.
For this example, we will enter an expression that evaluates to today's date for the "Departing" date. Choose the "expression" option from the "Input" command drop down and enter:
$date.toString("dd/MM/yy")
This expression will create the date in the "dd/MM/yy" format, which the Travelocity website can use. Please note the capital Ms (MM) for the month field. Do not use lowercase Ms, as that means 'minutes' in this function.
Please note, this expression will not enter the date correctly while you are building the project. You can switch to "Browse" mode and enter the date manually. However, this javascript expression will work correctly on the test run as well as on the actual runs on our servers.
10. Click on the "Select page" command + button that is located on the right of the command. From the toolbox that appears, choose the "Select" tool.
11. Click on the "Returning" date field.
12. An "Input" command will be created automatically. Let's assume that we want to set the "Returning" date field to 2 days from Today's date. This means that every time the project runs, the "Returning" date field will be set to 2 days after the run's date. To achieve this, choose the "expression" option from the "Input" command drop down and enter:
$date.addDays(2).toString("dd/MM/yy")
Please note, this expression will not enter the date correctly while you are building the project. You can switch to "Browse" mode and enter the date manually. However, this javascript expression will work correctly on the test run as well as on actual runs on our servers.
13. Now that you have all the input fields filled correctly, you can select the "Search" button and search for the results. Click on the "Select page" command + button that is located on the right of the command. From the toolbox that appears, choose the "Select" tool.
14. Click on the "Search" button on the web page to select it.
15. Click on the + button next to the "Select Search" and choose the "Click" command:
16. The "Click" command's configuration pop up will appear. When asked if this is a next page button, choose "No" and create a new template.
17. ParseHub will create the new template for you (Results template), and the results will appear on the web page.
18. To continue scraping the results from the web page on this new template, you can follow this article,which shows you how to use relative select to scrape multiple listings along with their details.