7

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.

5
  • read here to see how to format date time stackoverflow.com/questions/4802190/… see the Skilldrick answer Commented Jan 7, 2012 at 16:29
  • I updated my post please check it. Thanks Commented Jan 7, 2012 at 16:45
  • see my answer...PS: You forgot Saturday :P Commented Jan 7, 2012 at 17:16
  • Updated my answer. Please have a look. 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. Commented Jan 7, 2012 at 17:20

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

5 Comments

Please don't sign your posts.
After almost 2 years you have surely read meta.stackexchange.com/questions/2950/… by now?! Just write the answer. :)
The admins and mods support the "request", and the FAQ pretty much makes it a regulation anyway. The "request" part of the discussion on meta pertains more to the debate over whether I should have bothered editing your signature out.
Hmm, comment links are a bit hit-and-miss. I was linking to the mod comment on that question.
It's understood. Don't worry.
3

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.

Comments

2

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.

Comments

1
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

Comments

0

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

then you can do this

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

1 Comment

Please see my updated post this will make my question more clear. I want to get the day of the week and then display it in Sunday, Monday format.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.