-1

I'm looking for a way to convert javascript Date object to a text represent of the text like this:

Sunday the 26th of July two thousand and fifteen
Lucky
  • 14,677
  • 16
  • 104
  • 145
sd1sd1
  • 948
  • 3
  • 13
  • 27
  • Would format like Sun, 26 Jul 2015 10:42:40 GMT work? what's the range of years you expect? – Abhishek Bhardwaj Jul 26 '15 at 10:43
  • i work with bootstrap datepicker plugin. it alreadty gives me Sun, 26 Jul 2015 10:42:40 GMT - i want to create a full text representation. the range - is +- 100 years – sd1sd1 Jul 26 '15 at 10:44
  • possible duplicate of [Javascript library for human-friendly relative date formatting](http://stackoverflow.com/questions/7641791/javascript-library-for-human-friendly-relative-date-formatting) – ebram khalil Jul 26 '15 at 11:04
  • @sd1sd1 Please review the answer below, let me know in case you face any problem! – Abhishek Bhardwaj Jul 26 '15 at 11:29

2 Answers2

1

You can use the following code in JavaScript. Just pass the date object in function return_formatted_date, and this function will return text representation as you require. Hope this helps!

        <script>
            function return_formatted_date(date){
                var text = "";
                var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
                var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                var centuryText = ["One", "Two", "Three","Four", "Five", "Six", "Seven", "Eight", "Nine"];
                var eleven_twenty = ["Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"];
                var hundred_suffix = ["","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"];
                text = weekday[date.getDay()] + " the ";
                var day = date.getDate();
                if(day==1){
                    text = text +  day + "st";
                }else if(day==2){
                    text = text + day + "nd";
                }else if(day==3){
                    text = text + day + "rd";
                }else{
                    text = text + day + "th";
                }
                text = text + " of " + months[date.getMonth()];
                var year = parseInt(date.getFullYear());
                var century = parseInt(year/1000);
                var hundred = parseInt(year/100)%10;
                var tens = parseInt(year)%100;
                if(century!=0){
                    text = text + " " + centuryText[century-1] + " thousand";
                }
                if(hundred!=0){
                    text = text + " " + centuryText[hundred-1] + " hundred";
                }
                if(tens!=0){
                    text = text + " and";
                    if(tens<10){
                        text = text + " " + centuryText[tens-1];
                    }else if(tens>=11&&tens<=20){
                        text = text + " " + eleven_twenty[tens-11];
                    }else{
                        var tens_up = tens/10;
                        var tens_down = tens%10;
                        if(tens_up!=0){
                            text = text + " " + hundred_suffix[tens_up-1];
                        }
                        if(tens_down!=0){
                            text = text + " " + centuryText[tens_down-1];
                        }
                    }
                }
                return text;
            }

        </script>
Abhishek Bhardwaj
  • 222
  • 1
  • 2
  • 8
0

The only way to convert a java script date object to string (ie text) is like this:

var d = new Date();
var t = d.toString();

or by using:

var d = new Date();
var t = d.format("date-to-format");

I thinks this post will help you more:

How to format java script date

Community
  • 1
  • 1
Barr J
  • 9,047
  • 1
  • 22
  • 41