Help to write a function that from now will go back 10 years and return the date, time, year, week, and all about the date in English
2 answers
In JS, this can all be done simply using the Date object.
A simple example:
var now = new Date(); var newDate = new Date(); newDate.setFullYear(now.getFullYear() - 10); And then you refer to the date methods and take the data that you need, the list of methods is available from the link above, here are some of them:
getDate() Returns the day of the month (from 1-31) getDay() Returns the day of the week (from 0-6) getFullYear() Returns the year (four digits) getHours() Returns the hour (from 0-23) getMilliseconds() Returns the milliseconds (from 0-999) getMinutes() Returns the minutes (from 0-59) getMonth() Returns the month (from 0-11) getSeconds() Returns the seconds (from 0-59) Just keep in mind that some methods return data counting from 0, not 1, the same getHours or getMonth method, so to add the correct number, add another 1.
|
To manipulate the year:
To display localized data ("in English"):
Another way to format dates that are more productive if you need to work with a variety of dates is to compile the format in advance using:
var locale = 'en-US'; var d = new Date; d.setYear(d.getFullYear() - 10); console.log(d.toLocaleDateString(locale)); console.log(d.toLocaleTimeString(locale)); console.log(d.toLocaleString(locale, {weekday: 'long'})) var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; var dateTimeFormat = new Intl.DateTimeFormat(locale, options); console.log(dateTimeFormat.format(d)); - 2Attention, found the easiest way to return the 2007th. - Sasha Omelchenko
|