Getting day from date using jquery / javascript

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

$(document).ready(function() { var d = new Date(); alert(d.getDay()); } ); 

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

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

How can I get the day from the date in the above format?


This is what my function looks 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 transferring the date in the "YYYY-MM-DD" format, and it converts that date to a unix timestamp, and then I get the day, and then return the day like Sunday or Monday, etc. But here he only returns on Friday, which is wrong. When I change day to day, it should return the same day in the array.

+7
source share
6 answers

Date Object has a toString method that displays a UTC date string in the format you describe. However, the Date Object also contains many other methods.

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

 d = new Date( ).getDate( ) ; 

Strike>

A date string has a format that is not recognized by the Date string constructor. Check this line for return value:

 var nData = new Date(currentDate) ; 

He will return:

 "invalid Date" 

If your date format is ( d ),

 "dd-mm-yyyy" 

do it,

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

this should cause the Date Object to work. Or use the datepicker plugin for jQuery as described in this post.


Link to MDN documentation

+5
source

Instead of using the built-in Date to string transforms, which are implementation dependent, and instead of coding the logic yourself, it is best to use a suitable library. Here is an easy way to do this with 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 lead to an analysis of the input in the format YYYY / MM / DD (with obvious changes if your actual format is YYYY-MM-DD), more precisely, the year should be in 4 digits (yyyy) and month (M) and day ( d) can be in 1 or 2 digits (use MM and dd instead if you want to enter 2 digits). Then it records the date, so that only the day of the week is recorded as the word (dddd). If the input does not match the specified format, the function returns null, and for this case, of course, you should have the planned error handling.

As an added bonus, the code will be ready for globalization: if you ever need to change the code to create names in another language, you simply add one function call that sets the language (language) for Globalize.js.

+2
source

Relatively 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. Change http://jsfiddle.net/qgDHB/ to suit your needs.

I deleted some lines that are not needed.

+1
source
 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

+1
source

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

then you can do it

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

Inspired by the answer to globalization above, here's 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 loaded in a variety of formats (including string or secs-since-1970 or a JS Date object), and the choice of output format is equally comprehensive.

Sugar.js is 17.9K and gzipped (which includes all the other features that it adds, not just the date stuff); which 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.

0
source

All Articles