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.
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().
get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).
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.
The getDay() method returns the day of the week (0 to 6) of a date.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With