How to Print getday() function as Name by JavaScript

How to Print getday() function as Name by JavaScript

One of my client want to display estimate shipping day when an order received on static delivery page.

How to Print getday() function as Name by JavaScript

So, this can be done many way but its important to my because i am trying to display the functionality within Elementor HTML widget.

<script>
 * Function takes in a Date object and returns the day of the week in a text format.
 */
function getWeekDay(date){
    //Create an array containing each day, starting with Sunday.
    var weekdays = new Array(
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    );
    //Use the getDay() method to get the day.
    var day = date.getDay();
    //Return the element that corresponds to that index.
    return weekdays[day];
}
</script> 

The function takes in a Date object and returns the day of the week in text format. we will use getDay method to call the day.

Next, we can display the day

//The current weekday in a text format.
var date = new Date();
var weekDay = getWeekDay(date);
console.log('The current weekday is ' + weekDay);

The above code block will print Today text format

<script>
//Finding out what day a specific date fell on.
var date = new Date('December 25, 1987');
var weekDay = getWeekDay(date);
console.log('Christmas Day in 1987 fell on a ' + weekDay);
</script>

Above block quote will Print a specific day. you can also print birthday. cool right!!!

<script>
//What weekday is tomorrow?
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var weekDay = getWeekDay(tomorrow);
console.log('Tomorrow will be a ' + weekDay); 
</script>

The Above code block will print Tomorrow day. here i use it to my project. My client wanted to ship the product within 6 days. so i just change 1 with 6. So simple right?

if we want to use it display block should display with a id tag simply we can use below code

<script>
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 6);
var weekDay = getWeekDay(tomorrow);
console.log('Tomorrow will be a ' + weekDay);
  var weekDay = getWeekDay(tomorrow);
  document.getElementById("shippingday").innerHTML = weekDay;        
</script> 

The code will call the element by id now you can add the id to any html tag. i am adding the id in a paragraph tag

<p id="shippingday"></p>

Result:

the output day will be add 6 day that mean if today is Thursday so output will be Wednesday

Important note: the day only work within 7 day.

If you’re looking for the three-letter version of each day, then simply modify the array in the getWeekDay function.

Here are some of date related FAQ.

How to Get textual day of week?

By the Date.prototype.getDay() method to get the day of the week. This method returns a numeric value that is between 0 and 6. 0 is considered to be a Sunday and 6 is considered to be Saturday.

how to get local timezone javascript?

var offset = new Date().getTimezoneOffset(); console.log(offset);

How can display the JavaScript block to a id?

document.getElementById(“shippingday”).innerHTML = weekDay;

Hopefully, you found this function to be useful! Comment out if you have any question

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
1 Shares
Tweet
Share
Pin
Share1