最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to Get Only Day Name or Number in a Week in JavaScript Date() - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Oct 16, 2014 at 8:00 LeftyX 35.6k21 gold badges137 silver badges197 bronze badges asked Oct 16, 2014 at 7:36 SuffiiSuffii 5,78415 gold badges61 silver badges94 bronze badges 1
  • For date related stuff in JavaScript, your life will probably be easier using something like moment.js – Evan Knowles Commented Oct 16, 2014 at 7:41
Add a comment  | 

8 Answers 8

Reset to default 8

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()];

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()];

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()];

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.

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

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

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

You can try code here. It's very simple

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

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>

发布评论

评论列表(0)

  1. 暂无评论