1// `date` is a `Date` object
2const formatYmd = date => date.toISOString().slice(0, 10);
3
4// Example
5formatYmd(new Date()); // 2020-05-06
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
1 $.validator.addMethod("birth", function (value, element) {
2
3 var ActualDate = new Date();
4 var ActualYear = ActualDate.getFullYear();
5 var ActualMonth = ActualDate.getMonth();
6 var ActualDay = ActualDate.getUTCDate();
7
8 ActualMonth += 1; // O actual month vem sempre com menos um valor do verdadeiro mês
9
10 var Data_Inserida = value.split("-");
11 var year = parseInt(Data_Inserida[0], 10),
12 month = parseInt(Data_Inserida[1], 10),
13 day = parseInt(Data_Inserida[2], 10);
14
15 if (year == ActualYear) {
16
17 if (month == ActualMonth) {
18
19 if (day > ActualDay) {
20 return false;
21 } else
22 return true
23 } else {
24
25 if (month > ActualMonth)
26 return false;
27 else
28 return true;
29 }
30 }
31
32 // Máximo de idade neste caso é a idade de reforma, minímo de idade 18
33 if (year > (ActualYear - 66) && year < ActualYear-18) {
34 return true;
35 } else
36 return false;
37
38 return false;
39 })
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);
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 }