function setDate(obj, year) { obj.date = new Date(year); } var obj = {} setDate(obj, "2011, 0, 1, 0, 0, 0, 0"); console.log(obj.date + ''); 

  • And in what format is the date needed? - Alexey Shimansky
  • YYYY-MM-dd hh: mm: ss - YURII
  • @ Alexey Shimansky - in the format of an object of type Date :) - Igor
  • @YURII why did you decide to pass as the string "2011, 0, 1, 0, 0, 0, 0" ? What is the reason for this decision? - Alexey Shimansky
  • @ Alexey Shimansky, how else can you pass? - YURII

4 answers 4

https://www.w3schools.com/js/js_dates.asp

 function setDate(obj, strDate) { var parts = strDate.split(","); obj.date = new Date( +parts[0].trim(), +parts[1].trim(), +parts[2].trim(), // date +parts[3].trim(), +parts[4].trim(), +parts[5].trim(), +parts[6].trim() // time ); } var Building = {}; setDate(Building, "2011, 0, 1, 0, 0, 0, 0"); console.log(Building); 

  • The answer is not what it should be ... it was easier to make a кат ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ concatenation - Alexey Shimansky
  • one
    @ Alexey Shimansky hmm, my answer should have been just that. Otherwise, where did I get it from? - Igor
  • Well YYYY-MM-dd hh:mm:ss not the same as YYYY-MM-ddThh:mm:sssZ - Alexey Shimansky
  • @ Alexey Shimansky I am not a walker to discuss the difference between a Date object and a string with a date in some format. In the question: "how to get the date." - Igor
  • Probably for this, and there are comments to clarify something, right?) Ru.stackoverflow.com/questions/735732/… - Alexey Shimansky

 function d2(x) { return x<10 ? '0' + x : x; } function setDate(obj, year) { obj.date = new Date(year.replace( /^(\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)$/, function (match, y, M, d, H, m, s, z) { return y + '-' + d2(+M+1) + '-' + d2(d) + 'T' + d2(H) + ':' + d2(m) + ':' + d2(s) + '.' + z; } )); } var obj = {} setDate(obj, "2011, 0, 1, 0, 0, 0, 0"); console.log(obj.date + ''); 

    If you want to turn a string into a type of Date :

    ES6

     function setDate(obj, year) { obj.date = new Date(...year.split(',')); } var obj = {} setDate(obj, "2011, 0, 1, 0, 0, 0, 0"); console.log(obj.date + ''); 

    • Almost like mine ... Without ES6 sadness with this option ... - Qwertiy
    • yep in general, I came up with it earlier)) Igor just convinced me that I didn’t need a string in the format YYYY-MM-dd hh: mm: ss, but the object was Alexey Shimansky

    If you can use ES6, you can:

     function setDate(obj, year) { obj.date = new Date(...year.match(/\d+/g)); } var obj = {} setDate(obj, "2011, 0, 1, 0, 0, 0, 0"); console.log(obj.date + ''); 

    • Seven o'clock in the evening on December 31st, there is an intensive preparation for the New Year. - Igor
    • @Igor, I do not understand ... - Qwertiy
    • Sorry, in my head it seemed like a funny joke. - Igor
    • @Igor, by the way, I’ve got 3 nights on January 1st ... - Qwertiy
    • Aaa, I'm in EST - Eastern Standard Time. You have, therefore, the New Year has already arrived. - Igor