S aktuálnym naformátovaným časom to v js nie je vôbec také jednoduché. Preto je lepšie si len túto funkciu skopírovať.
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = yyyy + '-' + mm + '-' + dd;
console.log(formattedToday)
výsledok: 2022-11-8
Pre ďalšiu prácu s časom v javascripte je lepšie implementovať knižnicu pretože js ma veľmi zle natívne pracovanie s časom
Implementacia do Vue.js:
npm install moment --save
import moment from "moment";
console.log(moment().add(7, 'days').format('YYYY-MM-DD', ))
console.log(moment().format('YYYY-MM-DD', ))
alebo bez kniznice by to malo ist takto:
getCustomDate(days = 0) {
const today = new Date( Date.now() + days * 24 * 60 * 60 * 1000)
let yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedDay = yyyy + '-' + mm + '-' + dd;
return formattedDay
},
Pole vsetkych datumov:
dateArray: function getDates(startDate, stopDate) {
var dateArray = new Array();
var currentDate = startDate;
while (currentDate <= stopDate) {
dateArray.push(new Date(currentDate));
currentDate = currentDate.addDays(1);
}
return dateArray;
},