Possible Duplicate:
Formatting a date in javascript
I know other possible formats in JavaScript Date object but I did not get on how to format the date to MM/dd/yyyy HH:mm:ss
format.
Please let me know if you come across such problem.
This question is related to
javascript
date
date-format
You can always format a date by extracting the parts and combine them using string functions:
var date = new Date();_x000D_
var dateStr =_x000D_
("00" + (date.getMonth() + 1)).slice(-2) + "/" +_x000D_
("00" + date.getDate()).slice(-2) + "/" +_x000D_
date.getFullYear() + " " +_x000D_
("00" + date.getHours()).slice(-2) + ":" +_x000D_
("00" + date.getMinutes()).slice(-2) + ":" +_x000D_
("00" + date.getSeconds()).slice(-2);_x000D_
console.log(dateStr);
_x000D_
var d = new Date(); // calling the function formatDate(d,4); function formatDate(dateObj,format) { var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var curr_date = dateObj.getDate(); var curr_month = dateObj.getMonth(); curr_month = curr_month + 1; var curr_year = dateObj.getFullYear(); var curr_min = dateObj.getMinutes(); var curr_hr= dateObj.getHours(); var curr_sc= dateObj.getSeconds(); if(curr_month.toString().length == 1) curr_month = '0' + curr_month; if(curr_date.toString().length == 1) curr_date = '0' + curr_date; if(curr_hr.toString().length == 1) curr_hr = '0' + curr_hr; if(curr_min.toString().length == 1) curr_min = '0' + curr_min; if(format ==1)//dd-mm-yyyy { return curr_date + "-"+curr_month+ "-"+curr_year; } else if(format ==2)//yyyy-mm-dd { return curr_year + "-"+curr_month+ "-"+curr_date; } else if(format ==3)//dd/mm/yyyy { return curr_date + "/"+curr_month+ "/"+curr_year; } else if(format ==4)// MM/dd/yyyy HH:mm:ss { return curr_month+"/"+curr_date +"/"+curr_year+ " "+curr_hr+":"+curr_min+":"+curr_sc; } }
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
using this you can format date.
you can change the appearance in the way you want then
for more info you can visit here
Source: Stackoverflow.com