Click here to Skip to main content
15,890,409 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Maybe this question asked before but surprisingly i search a lot didn't find any solution
I have this date

JavaScript
var currentDate = new Date();


Tue Jan 06 2015 10:40:00 GMT+0500 (Pakistan Standard Time)

How can i get only full date from it (not day\time) only Date(bold part of above line)
Posted

Try this:
JavaScript
var currentDate = new Date();
var options = { month: "short", year: "numeric", day: "2-digit" };
var formattedDate = currentDate.toLocaleDateString("en-US", options);
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 1-Jan-15 10:18am    
what is this is there not a single function available to solve this problem
Thomas Daniels 1-Jan-15 10:19am    
There is: the toLocaleDateString function, as I said in my answer.
Use following code:

JavaScript
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var formatedDate = curr_date + "-" + curr_month + "-" + curr_year ;


For more look at:
http://www.webdevelopersnotes.com/tips/html/javascript_date_and_time.php3[^]
Thanks.
 
Share this answer
 
v3
Muhamad there is no specific function to get the date only.
getDate();<br />
getMonth();<br />
getFullYear();

are the methods which you can use to generate your data. You can write a common function and add in the Scripts(like GlobalFunction.js) and reuse in every section where the only date retreival function is needed.
You can use the method Snesh has mentioned and also if you want the Names to be displayed
then use like below,
JavaScript
var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];

document.write("The current month is " + monthNames[d.getMonth()]);
//Remember getMonth() for january is 0! so we use '+1', here array is used so [0] is january.
var dayNames = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday"];
var d = new Date();
document.write(dateNames[d.getDate()]+"/"+monthNames[d.getMonth()]+"/"+d.getFullYear())

I hope this helps.
Please post back queries if any.
Thanks.
 
Share this answer
 
it's moment.js[^] you need.

http://jsfiddle.net/vpzv9ohz/[^]
var dateTimeMoment = moment(new Date());
var dateTimeString = dateTimeMoment.format("MMM DD YYYY");
alert(dateTimeString);
 
Share this answer
 
Technically, you can't - a Date object is a number of milliseconds since a specific point in time, so you can't have a Date that doesn't have a time component.

But I suspect that what you want to do is show a date without a time to the user, and that's easy - convert it to a string using toDateString[^]
 
Share this answer
 
Comments
Muhamad Faizan Khan 1-Jan-15 10:31am    
i have used it it also show me the day name

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900