{"id":1094,"date":"2023-09-22T14:49:00","date_gmt":"2023-09-22T14:49:00","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1094"},"modified":"2023-10-01T14:54:36","modified_gmt":"2023-10-01T14:54:36","slug":"how-can-i-use-javascript-to-find-day-name-from-a-specific-date","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-can-i-use-javascript-to-find-day-name-from-a-specific-date\/","title":{"rendered":"How Can I Use JavaScript to Find Day Name From a Specific Date?"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-5.png\" alt=\"\" class=\"wp-image-1096\" width=\"514\" height=\"196\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-5.png 776w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-5-300x114.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-5-768x293.png 768w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/figure>\n\n\n\n<p>JavaScript, one of the core technologies of web development, is a versatile language that can perform a wide range of tasks, including handling date and time operations. One common requirement in web applications is to determine the day of the week (e.g., Monday, Tuesday, etc.) for a given date. Here we use JavaScript to find the day name from a specific date, whether it&#8217;s for displaying events, scheduling tasks, or creating dynamic calendars.<\/p>\n\n\n\n<p><strong>JavaScript&#8217;s Date Object<\/strong><\/p>\n\n\n\n<p>JavaScript provides a built-in <code>Date<\/code> object that makes working with dates and times relatively straightforward. To find the day name from a specific date, follow these steps:<\/p>\n\n\n\n<p><strong>1. Create a Date Object<\/strong><\/p>\n\n\n\n<p>To start, create a <code>Date<\/code> object and provide the specific date you want to work with. You can provide the date in various formats, including ISO 8601, timestamp, or as separate year, month, and day values.<\/p>\n\n\n\n<p>Here&#8217;s an example using ISO 8601 format (YYYY-MM-DD)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const date = new Date('2023-10-01');\r\n<\/code><\/pre>\n\n\n\n<p><strong>2. Get the Day of the Week<\/strong><\/p>\n\n\n\n<p>JavaScript&#8217;s <code>Date<\/code> object has a method called <code>getDay()<\/code> that returns the day of the week as an integer, where Sunday is 0, Monday is 1, and so on, up to Saturday as 6.<\/p>\n\n\n\n<p>To get the day name, you can create an array of day names and use the <code>getDay()<\/code> result as an index to retrieve the corresponding day name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const daysOfWeek = &#91;'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];\r\nconst dayName = daysOfWeek&#91;date.getDay()];\r\n<\/code><\/pre>\n\n\n\n<p><strong>Putting It All Together<\/strong><\/p>\n\n\n\n<p>Here&#8217;s the complete JavaScript code to find the day name from a specific date.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getDayNameFromDate(inputDate) {\r\n    const date = new Date(inputDate);\r\n    const daysOfWeek = &#91;'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];\r\n    const dayName = daysOfWeek&#91;date.getDay()];\r\n    return dayName;\r\n}\r\n\r\n\/\/ Example usage:\r\nconst dateToCheck = '2023-10-01';\r\nconst dayName = getDayNameFromDate(dateToCheck);\r\nconsole.log(`The day on ${dateToCheck} is ${dayName}.`);\r\n<\/code><\/pre>\n\n\n\n<p>The <code>getDayNameFromDate<\/code> function takes an input date, creates a <code>Date<\/code> object, and returns the corresponding day name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript, one of the core technologies of web development, is a versatile language that can perform a wide range of tasks, including handling date and time operations&#8230;. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[403,45,404],"class_list":["post-1094","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-how-can-i-use-javascript-to-find-day-name-from-a-specific-date","tag-javascript","tag-javascripts-date-object"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1097,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1097"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}