Suffii
Suffii

Reputation: 5784

How to Get Only Day Name or Number in a Week in JavaScript Date()

I am using following code snippets to get the First and last Day of month which working fine but what I need is getting only the Name string like "Fri", "Wed", "Sun" and the code is returning a long Date format as:

Fri Oct 31 2014 00:00:00 GMT-0700 (Pacific Daylight Time)

var today = new Date();
var lastOfMonth = new Date(today.getFullYear(),today.getMonth()+1, 0);

var today = new Date();
var firstOfMonth = new Date(today.getFullYear(),today.getMonth(), 1);

Can you please let me know how I can get ONLY the Name or Number of the day in a week (using getDay()) of the First day?

Upvotes: 6

Views: 17946

Answers (8)

GMKHussain
GMKHussain

Reputation: 4671

Reusable and simpler approach with two example

  1. with callback function for DOM, we can call any function.
  2. simple usage just call getDayNameByDate('YYYY-MM-DD') or getDayNameByDate('MM-DD-YYYY')

const getDayNameByDate = (date , callback ) => {
  const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
  const dayIndex = new Date( date ).getDay()
  const res = days[dayIndex]; 
  if(callback) { callback(res) }
  return res
}


/* optional function */
const showDom = async ( res ) => {
  const output = await document.getElementById('output');
  output.innerHTML = res
}



console.log(
 'Today day is: ', getDayNameByDate( new Date() )
)
<p>Change date and see result</p>
<input placeholder='YYYY-MM-DD or MM-DD-YYYY' onkeyup="getDayNameByDate(this.value, showDom) " />
<p>This day is: <b id="output">N/A</b></p>

Upvotes: 0

Fatima Zohra
Fatima Zohra

Reputation: 2967

You can define arrays

var Months = ['Jan','Feb'....];
var Days = ['Sun','Mon','Tues','Wednes','Thurs','Fri','Sat'];

var firstOfMonth = new Date(today.getFullYear(),Months[today.getMonth()], 1);

var Day = Days[today.getDay()];

Upvotes: 3

Ryan Nghiem
Ryan Nghiem

Reputation: 2438

You can try code here. It's very simple

lastOfMonth.toString().split(' ')[0]

Upvotes: 0

dreyescat
dreyescat

Reputation: 13798

If what you want is exactly the week of day how it appears in the Date format you can:

firstOfMonth.toString().split(' ')[0]

Upvotes: 0

Use haven't use getDay

today.getDay();

first of month

 firstOfMonth.getDay();

last of month

 lastOfMonth.getDay();

or if u want to get dayname use

var days = ['Sun','Mon','Tues','Wed','Thrus','Fri','Sat'];
//First of month
days[firstOfMonth.getDay()];
//last of month
days[lastOfMonth.getDay()];

Upvotes: 8

Mohammad Ashfaq
Mohammad Ashfaq

Reputation: 1373

var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
alert(days[today.getDay() - 1]);

Upvotes: 0

itsmichaelwang
itsmichaelwang

Reputation: 2328

The getDay() function in JavaScript returns a number from 0-6 depending on the day of the week, so maybe just make an array like:

weekDay = [Sun Mon Tue Wed Thur Fri Sat]

And then weekDay[your_date_object.getDay()] would do the trick. You can wrap it all in a function for better usability.

Upvotes: 0

Barry
Barry

Reputation: 3723

Use

lastOfMonth.getDay()

to get the day number.

You can use an array like

var weekday = new Array(7);
weekday[0]=  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

to get the written day with

var writtenDay = weekday[lastOfMonth.getDay()];

Upvotes: 1

Related Questions