-4

i want to use date function in javascript to get date as Sunday, September 8 at 11:46am

below is the code i am using -: var currentTime = new Date()

var dd1 = currentTime.getDate();
var mm1 = currentTime.getMonth()+1; // January is 0!
var yyyy1 = currentTime.getFullYear();

if(dd1 < 10)
{
    dd1 = '0'+ dd1;
}
if(mm1 < 10)
{
    mm1 = '0' + mm1;
}
var date = dd1 + '/' + mm1 + '/' + yyyy1;

but this gives out put as -: 08/09/2013 instead i want to get it as -: Sunday, September 8 at 11:46am

A.L
  • 10,259
  • 10
  • 67
  • 98
Abhinav
  • 691
  • 1
  • 6
  • 20

4 Answers4

2

The following sample code will be helpful:

<!DOCTYPE HTML>
<html>
<body>
<script>

    var current_date = new Date ( );
    var month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var day_names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    var hours = current_date.getHours();
    var minutes = current_date.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;

    // Display date in required format
    var formatted_date = day_names[current_date.getDay()] + ", " + month_names[current_date.getMonth()] + " " + current_date.getDate() + " at " + " " + hours + ":" + minutes +  ampm ;
    console.log( formatted_date );

</script>

</body>
</html>

Resources:

Working with date formatting in javascript

Display javascript datetime in 12 hour AM/PM format

Community
  • 1
  • 1
  • the algorithm looks correct (good job), but please first let someone review your code. This is a bad way to create an array of constants (see [7's answer](http://stackoverflow.com/a/18681183/499214)), and `document.write` is downright bad (I doubt the asker is doing it during the initial page load, and even then DOM manipulation works just as fine and is more flexible). You can use `console.log` to demonstrate the usage of some variable instead - note that you'll have to concatenate it first. – John Dvorak Sep 08 '13 at 06:58
  • Yes you are right about the way code is written, but I have just shown a way. I know this is not an optimal solution. It is just a way of formatting the date in pure JavaScript. – Sameer Mahant Sep 08 '13 at 07:03
  • You should fix the code to use good coding practices (use an array literal and remove `document.write`), then I'll upvote. – John Dvorak Sep 08 '13 at 07:04
  • If you don't, would you mind if I fix that for you when I get home? I don't want to shoot down an answer that would otherwise be useful just for the bad coding style it promotes. – John Dvorak Sep 08 '13 at 07:08
  • thanks alot friends and special thanks to Sameer Mahant the script provided by u works and gives the output as i need. thanks alot – Abhinav Sep 08 '13 at 07:27
  • @user1659298 don't forget to mark the answer as accepted if it solves your problem. I've edited in another small fix, though. – John Dvorak Sep 08 '13 at 10:36
  • thanks Jan Dvorak .... will keep this in mind from next time....accepted the solution provided by sameer – Abhinav Sep 08 '13 at 17:31
1

Try,

var date = new Date( dd1 + '/' + mm1 + '/' + yyyy1);

instead

 var date = dd1 + '/' + mm1 + '/' + yyyy1;

Little messy

var date = new Date  ( dd1 + '/' + mm1 + '/' + yyyy1);
var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][date.getDay()];
var monthName = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ][date.getMonth()];
alert (name + " , "  + monthName + " " + date.getDate()+ " at " + "  your time as you like");
internals-in
  • 4,798
  • 2
  • 21
  • 38
0

you need to use a good JavaScript date formatting library. Try moment.js You will have to read their formatting docs to figure out how to do what you need - but you can do it

mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • 1
    At 15k you should know the stance on link-only answers. Code samples, please? – John Dvorak Sep 08 '13 at 06:33
  • 1
    at 12k you should know that i am not required to hand the OP code on silver platter – mkoryak Sep 08 '13 at 06:34
  • It is link-only. It is also technically incorrect (it can well be done without a library), but that's beside the point. You don't have to hand the OP code, but just pointing to a library is not useful. At the very least, you could quote the relevant documentation here. Even pointing to the documentation would be more helpful than this. – John Dvorak Sep 08 '13 at 06:35
  • fair enough, but its its 2:30am down here and this is the best i can do. no one else seems to know any better – mkoryak Sep 08 '13 at 06:36
  • Also note the tag wiki states "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected.". – John Dvorak Sep 08 '13 at 06:37
  • note that you serve as an example for newbie answerers. You _should_ give a good example (even if only in the morning - please ping me then so I can upvote) – John Dvorak Sep 08 '13 at 06:40
  • I don't have the time now, but I can post a no-library answer in a few hours when I return. – John Dvorak Sep 08 '13 at 06:50
  • you didnt post it, its been a few years!! – mkoryak Dec 06 '14 at 01:58
0

Perhaps toDateString function would help

var objDate = new Date();
var strDate = objDate.toDateString();
var intHrs = objDate.getHours();
var intMin = objDate.getMinutes();

alert(strDate + ' ' + intHrs + ' : ' + intMins); 

Complete reference on Date object from MDN available here