1function formatDate(date) {
2 var d = new Date(date),
3 month = '' + (d.getMonth() + 1),
4 day = '' + d.getDate(),
5 year = d.getFullYear();
6
7 if (month.length < 2) month = '0' + month;
8 if (day.length < 2) day = '0' + day;
9
10 return [year, month, day].join('-');
11 }
12 alert(formatDate('05/08/2015'));
13
1var formattedDate = new Date("yourUnformattedOriginalDate");
2var d = formattedDate.getDate();
3var m = formattedDate.getMonth();
4m += 1; // JavaScript months are 0-11
5var y = formattedDate.getFullYear();
6
7$("#txtDate").val(d + "." + m + "." + y);
1var date_format = new Date();
2
3document.write(innerHTML = date_format.getMonth()+'/'+ date_format.getDate()+'/'+date_format.getFullYear());
1var dateString = "10/23/2015"; // Oct 23
2
3var dateObject = new Date(dateString);
4
5document.body.innerHTML = dateObject.toString();
1$.date = function(dateObject) {
2 var d = new Date(dateObject);
3 var day = d.getDate();
4 var month = d.getMonth() + 1;
5 var year = d.getFullYear();
6 if (day < 10) {
7 day = "0" + day;
8 }
9 if (month < 10) {
10 month = "0" + month;
11 }
12 var date = day + "/" + month + "/" + year;
13
14 return date;
15};
16
1GetDatePattern("MM/dd/yyyy");
2 function GetDatePattern(pattern)
3 {
4 var monthNames=["January", "February", "March", "April", "May", "June",
5 "July", "August", "September", "October", "November", "December"];
6
7 var todayDate = new Date();
8
9 var date = todayDate.getDate().toString();
10 var month = todayDate.getMonth().toString();
11 var year = todayDate.getFullYear().toString();
12 var formattedMonth = (todayDate.getMonth() < 10) ? "0" + month : month;
13 var formattedDay = (todayDate.getDate() < 10) ? "0" + date : date;
14 var result = "";
15
16 switch (pattern) {
17 case "M/d/yyyy":
18 formattedMonth = formattedMonth.indexOf("0") == 0 ? formattedMonth.substring(1, 2) : formattedMonth;
19 formattedDay = formattedDay.indexOf("0") == 0 ? formattedDay.substring(1, 2) : formattedDay;
20
21 result = formattedMonth + '/' + formattedDay + '/' + year;
22 break;
23
24 case "M/d/yy":
25 formattedMonth = formattedMonth.indexOf("0") == 0 ? formattedMonth.substring(1, 2) : formattedMonth;
26 formattedDay = formattedDay.indexOf("0") == 0 ? formattedDay.substring(1, 2) : formattedDay;
27 result = formattedMonth + '/' + formattedDay + '/' + year.substr(2);
28 break;
29
30 case "MM/dd/yy":
31 result = formattedMonth + '/' + formattedDay + '/' + year.substr(2);
32 break;
33
34 case "MM/dd/yyyy":
35 result = formattedMonth + '/' + formattedDay + '/' + year;
36 break;
37
38 case "yy/MM/dd":
39 result = year.substr(2) + '/' + formattedMonth + '/' + formattedDay;
40 break;
41
42
43 case "yyyy-MM-dd":
44 result = year + '-' + formattedMonth + '-' + formattedDay;
45 break;
46
47 case "dd-MMM-yy":
48 result = formattedDay + '-' + monthNames[todayDate.getMonth()].substr(3) + '-' + year.substr(2);
49 break;
50
51 case "MMMM d, yyyy":
52 result = todayDate.toLocaleDateString("en-us", { day: 'numeric', month: 'long', year: 'numeric' });
53 break;
54
55
56 }
57 }