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
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
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>
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: