admin管理员组

文章数量:1208155

I have a date in this formate "YYYY/MM/DD". Now I want to get the day from this format. How can I get it using javascript or jquery? One way is this

 $(document).ready(function() {

var d = new Date();

alert(d.getDay());
 } );

but the problem here is that d variable contains date in this format

Sat Jan 07 2012 18:16:57 GMT+0200 (FLE Standard Time)

How can I get day from date in above format?


Here is how my function look like

function dayOfWeek(d) {
            var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Sunday');
            var currentDate = Math.round(+new Date(d)/1000);
            var nData = new Date(currentDate);
            //alert(nData);
            //alert(nData.getDay());
            //var day = d.split("-")[2];
            //var t = day.replace("0","");
            //alert(t);
            return dayNames[nData.getDay()];
        }

What I am doing is I am passing date in "YYYY-MM-DD" format and it converts that date to unix timestamp and then I get the day and then return the day as Sunday or Monday etc. But here it only returns Friday which is incorrect. When I change the day in date it should return the same day in array.

I have a date in this formate "YYYY/MM/DD". Now I want to get the day from this format. How can I get it using javascript or jquery? One way is this

 $(document).ready(function() {

var d = new Date();

alert(d.getDay());
 } );

but the problem here is that d variable contains date in this format

Sat Jan 07 2012 18:16:57 GMT+0200 (FLE Standard Time)

How can I get day from date in above format?


Here is how my function look like

function dayOfWeek(d) {
            var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Sunday');
            var currentDate = Math.round(+new Date(d)/1000);
            var nData = new Date(currentDate);
            //alert(nData);
            //alert(nData.getDay());
            //var day = d.split("-")[2];
            //var t = day.replace("0","");
            //alert(t);
            return dayNames[nData.getDay()];
        }

What I am doing is I am passing date in "YYYY-MM-DD" format and it converts that date to unix timestamp and then I get the day and then return the day as Sunday or Monday etc. But here it only returns Friday which is incorrect. When I change the day in date it should return the same day in array.

Share Improve this question edited Jan 7, 2012 at 17:27 Lightness Races in Orbit 385k77 gold badges664 silver badges1.1k bronze badges asked Jan 7, 2012 at 16:22 Om3gaOm3ga 32.8k45 gold badges149 silver badges230 bronze badges 5
  • read here to see how to format date time stackoverflow.com/questions/4802190/… see the Skilldrick answer – Snake Eyes Commented Jan 7, 2012 at 16:29
  • I updated my post please check it. Thanks – Om3ga Commented Jan 7, 2012 at 16:45
  • see my answer...PS: You forgot Saturday :P – Snake Eyes Commented Jan 7, 2012 at 17:16
  • Updated my answer. Please have a look. – FK82 Commented Jan 7, 2012 at 17:19
  • the problem here is that d variable contains date in this format: Sat Jan 07 2012 18:16:57 GMT+0200 (FLE Standard Time); nah -- consider it as storing the date in some internal binary format. It only looks like that by default when you stringize it. – Lightness Races in Orbit Commented Jan 7, 2012 at 17:20
Add a comment  | 

6 Answers 6

Reset to default 5

The Date Object has a toString method which outputs a UTC date string in the format you described. However the Date Object also has a host of other methods.

To get the day of the month (1-31) you use the getDate method:

d = new Date( ).getDate( ) ;

Your date string has a format that will not get recognized by the Date string constructor. Test this line for its return value:

var nData = new Date(currentDate) ;

It will return:

"invalid Date"

If your date format (d) is this,

"dd-mm-yyyy"

do this,

var a = d.split("-") ;
var date = new Date( a[2], (a[1] - 1), a[0] ) ;

this should net you a working Date Object. Or use the datepicker plug-in for jQuery as described in this post.


Link to MDN documentation

Regarding to new updated code:

$(function()
  {
$("a").click(function () {
            var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
            var nData = new Date();
            //alert(nData);
            //alert(nData.getDay());
            //var day = d.split("-")[2];
            //var t = day.replace("0","");
            //alert(t);
            alert(dayNames[nData.getDay()]);
        });
});

the above code is an example. Edit http://jsfiddle.net/qgDHB/ with your needs.

I removed some lines which are not needed.

Instead of using built-in Date to string conversions, which are implementation-dependent, and instead of coding the logic yourself, it is best to use a suitable library. Here’s one simple way of doing it using Globalize.js:

function dayOfWeek(s) {
  var d = Globalize.parseDate(s, 'yyyy/M/d');
  if(d == null) { 
    return null;
  } else {
   return Globalize.format(d, 'dddd');
  }
}

This will parse input in the format YYYY/MM/DD (with obvious modifications if your actual format is YYYY-MM-DD), more exactly so that the year is in 4 digits (yyyy) and the month (M) and day (d) may be in 1 or 2 digits (use MM and dd instead if you wish to enforce 2 digits). Then it writes the date so that only the day of the week is written, as a word (dddd). If the input is not of the specified format, the function returns null, and you should of course have some planned error handling for this case.

As an extra bonus, the code will be globalization-ready: if you ever need to modify the code to produce the names in another language, you just add one function invocation that sets the locale (language) for Globalize.js.

var d=new Date();
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";

var n = weekday[d.getDay()];

check

Is the "format" you are talking about a regular string?

then you can do this

var theDay = theDate.split("/")[2];

Inspired by the Globalize answer above, here is how to do it in the Sugar.js library:

<script src="sugar.min.js"></script>
...
var test = Date.create(d);
console.log(test.format("{Weekday}"));

d can be in loads of different formats (including string or secs-since-1970 or JS Date object) and the output format choices are equally comprehensive.

Sugar.js is 17.9K minified and gzipped (which includes all the other features it adds, not just the date stuff); that is about twice the size of Moment.js, the alternative I was considering. But Moment.js does not have the equivalent of the {dow} or {weekday} formats.

本文标签: Getting day from date using jqueryjavascriptStack Overflow