Convert date format to jquery

I need a date to display in this format 2014-11-04 as "yy mm dd"

Currently my script is still showing me Tue Nov 04 2014 00:00:00 GMT + 0200 (standard time in Egypt)

$(document).ready(function() { var userDate = '04.11.2014'; from = userDate.split("."); f = new Date(from[2], from[1] - 1, from[0]); console.log(f); }); 
+5
source share
3 answers

You can build it using date object methods

 var date = new Date(userDate), yr = date.getFullYear(), month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(), day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(), newDate = yr + '-' + month + '-' + day; console.log(newDate); 
+11
source

You can try the following:

  $(document).ready(function() { var userDate = '04.11.2014'; var from = userDate.split("."); var f = new Date(from[2], from[1], from[0]); var date_string = f.getFullYear() + " " + f.getMonth() + " " + f.getDate(); console.log(date_string); }); 

Alternatively, I would look at Moment.js. It would be easier to deal with dates:

 $(document).ready(function() { var userDate = '04.11.2014'; var date_string = moment(userDate, "DD.MM.YYYY").format("YYYY-MM-DD"); $("#results").html(date_string); }); 

MOMENT.JS DEMO: FIDDLE

+4
source

I think you could find the answer here: Converting a string to date in js

Replace "." with "-" to confirm the date.

Edit: this is executed in javascript, jquery does not have utillity function for date

0
source

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


All Articles