Change date format from jquery

I have this date that I get from jquery

Wed Oct 30 2013 09:05:17 GMT-0800 (Hora estándar Pacífico (México)) 

what i get this function

 var date = new Date(); var newdate = new Date(date); newdate.setDate(newdate.getDate() + 7); var nd = new Date(newdate); $('#vigencia_receta_11').val(nd); 

But I only need the date, not the time, I want to format the date as "DD / MM / YYYY"

+4
source share
3 answers

I liked the following code:

 function myDateFormatter ("pass your date here") { var d = new Date(dateObject); var day = d.getDate(); var month = d.getMonth() + 1; var year = d.getFullYear(); if (day < 10) { day = "0" + day; } if (month < 10) { month = "0" + month; } var date = day + "/" + month + "/" + year; return date; }; 
+2
source

A couple of parameters.

If you're okay, including jQueryUI: $("#vigencia_receta_11").val($.datepicker.formatDate('dd/M/yy', nd));

Otherwise, the jQuery dateFormat plugin does something similar: $("#vigencia_receta_11").val($.format.date(nd, 'dd/M/yy'));

+5
source

The date object has functions for accessing individual date components. You can use:

 $('#vigencia_receta_11').val((nd.getMonth() + 1) + "/" + nd.getDate() + "/" + nd.getFullYear()); 

Note that getMonth () returns a date with a zero index, so you need to add 1 to get it in a format convenient for people.

+2
source

Source: https://habr.com/ru/post/1499974/


All Articles