smj2393
smj2393

Reputation: 1957

Convert a date 2013-11-05 to Tuesday, 5th November, 2013 in JavaScript?

I have had a look at datejs, but I cant get it to output the correct format.

It outputs "Nov" instead of "November" and adds the time "00:00:00" onto the end of the conversion.

Date.parse('2013-11-05') outputs Tue Nov 05 2013 00:00:00

Any help would be great thanks.

Upvotes: 2

Views: 455

Answers (4)

varun
varun

Reputation: 76

You can do something like this

`

$(document).ready(function(){
var date=  new Date(); 
 var _strsplit= date.toDateString().split(" ");

 var Dayname=_strsplit[0];

 var MonthName=_strsplit[1];

 var Day=_strsplit[2];

 var Year=_strsplit[3];

 switch(Dayname)
 {

  case "Mon":
        Dayname="Monday";
        break;
         case "Tue":
        Dayname="Tuesday";
        break;

         case "Wed":
        Dayname="Wednesday";
        break;
         case "Thr":
        Dayname="Thrusday";
        break;
   case "Fri":
        Dayname="Friday";
        break;
         case "Sat":
        Dayname="Saturday";
        break;
         case "Sun":
        Dayname="Sunday";
        break;

 }
 switch(MonthName)
 {

 case "Nov":
      MonthName="Novemeber";
      break;

//do same for month's

 }

 document.write(Dayname +" "+MonthName+" "+Day+" "+Year);



}); 
</script>`

Upvotes: 0

Krish R
Krish R

Reputation: 22721

Can you try this, below function give outputs is Tuesday, 5th November, 2013

                var dateStr='2013-11-05';                    

                var str = DateConvert(dateStr);
                window.alert(str);    

                  function DateConvert(dateStr){          

                    var d = new Date(dateStr);
                    var month = ["January", "February", "March", "April", "May", "June",
                                 "July", "August", "September", "October", "November", "December" ][d.getMonth()];

                    var day = d.getDate();
                     var suffix = 'th';
                     switch (day) {
                         case 1:
                         case 21:
                         case 31:
                             suffix =  'st';                                
                         case 2:
                         case 22:
                             suffix = 'nd';                             
                         case 3:
                         case 23:                                
                             suffix = 'rd';
                       }                         


                     var DayName = ["Sunday", "Monday", "Tuesday", "Wednessday", "Thursday", "Friday", "Saturday"][d.getDay()];                      

                     var str = DayName+', '+day+suffix+' '+ month+', '+ d.getFullYear();

                  return str;
               }

Upvotes: 0

user2864740
user2864740

Reputation: 61975

Date.parse is not reliable in pre-ES5 or with formats outside a simplified version of ISO8601. In addition, Date.toDateString is also implementation-dependent.

I prefer Moment.js because it's small, well-documented, and quite robust - plus, I dislike writing date parsing and formatting by hand. Something like this (fiddle) ought to do the job:

var m = moment('2013-11-05')
var str = m.format('dddd, Do MMMM, YYYY')

(Moment.js also supports localization, if you're into that sort of thing.)

Upvotes: 5

Shivam
Shivam

Reputation: 714

Please try the given code probably it would help you.

<script type="javascript">
   var dateTime = new Date(); 
   alert(dateTime.toString());
</script>

Upvotes: -1

Related Questions