Recipe 13Formatting Dates with Intl.DateTimeFormat()

Task

Suppose you work as a programmer for an online shop that delivers products after two days from the date of purchase. Your task is to create a JavaScript code that will notify the user about the exact day of the week when they can expect to receive their ordered goods.

For instance, if a customer purchases a product on a Saturday, your code should inform them that the delivery will take place on the following Monday.

Solution

This task involves three steps:

  • Getting the current date
  • Adding two days to the current date
  • Converting that date to the day of the week

We can perform the first two steps with a function like this:

 function​ addDaysToToday(days) {
 const​ d = ​new​ Date();
  d.setDate(d.getDate() ...

Get Text Processing with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.