Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting day from date using jquery/javascript

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.

like image 978
Om3ga Avatar asked Jan 07 '12 16:01

Om3ga


People also ask

How can get today day in jquery?

FYI - getDay() will give you the day of the week... ie: if today is Thursday, it will return the number 4 (being the 4th day of the week). To get a proper day of the month, use getDate().

How can I get current date in jquery in dd mm yyyy format?

get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).

How do I get today's date in JavaScript?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.

How do you find the day of the week from a date?

The getDay() method returns the day of the week (0 to 6) of a date.


4 Answers

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

like image 122
FK82 Avatar answered Oct 20 '22 22:10

FK82


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.

like image 32
Snake Eyes Avatar answered Oct 20 '22 21:10

Snake Eyes


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.

like image 2
Jukka K. Korpela Avatar answered Oct 20 '22 21:10

Jukka K. Korpela


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

like image 1
Gadde Avatar answered Oct 20 '22 20:10

Gadde