In js how can I get the timestamp of a date like 2 weeks ago. For example I get the current timestamp, Then I need to get the timestamp 3 days ago, or 2 weeks ago, or a month ago. Is there a moment library that does this? I cant find anything online.
Asked
Active
Viewed 120 times
-1
-
1https://momentjs.com/ ? – Miro Feb 21 '21 at 08:16
-
1Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – Tigger Feb 21 '21 at 08:18
2 Answers
1
As the comments say, check out Moment JS: https://momentjs.com/
moment().subtract(10, 'days').calendar(); // 02/11/2021
moment().subtract(6, 'days').calendar(); // Last Monday at 12:17 AM
moment().subtract(3, 'days').calendar(); // Last Thursday at 12:17 AM
moment().subtract(1, 'days').calendar(); // Yesterday at 12:17 AM
moment().calendar(); // Today at 12:17 AM
moment().add(1, 'days').calendar(); // Tomorrow at 12:17 AM
moment().add(3, 'days').calendar(); // Wednesday at 12:17 AM
moment().add(10, 'days').calendar();
moment().format(); // 2021-02-21T00:43:34-08:00
Try:
moment().subtract(6, 'days').format()

Da Mahdi03
- 1,468
- 1
- 9
- 18
-
Is there a way to get the timestamp from this? Instead of it outputting the date string? – ousmane784 Feb 21 '21 at 08:22
-
what do you mean by timestamp and not "data string"? using `moment.format()` it returns an ISO string, and you can get that into a JS date by doing `new Date(moment.format())` if you wanted to use the regular JS Date API after using moment to go back a certain interval of time – Da Mahdi03 Feb 21 '21 at 08:25
-
I need the timestamp and not the text string 'Last Monday at 12:17 AM', is there a way to do .calendar().timestamp? I cant find it in the moment docs. I tried also wrapping your answer in new Date(moment().subtract(10, 'days').calendar()) but its giving an error that its not a valid date – ousmane784 Feb 21 '21 at 08:31
-
`new Date(moment().subtract(10, 'days').calendar().format())` should work. Check out the Date API and the Moment API websites for more clarification on what you're looking for – Da Mahdi03 Feb 21 '21 at 08:34
-
-
0
Handling date objects in javascript is bit tricky compared to other general purpose languages. However this could be achieved in Java script's Date.parse(date)
method.
This method returns the timespan between the given date and 1970-01-01 in milliseconds.
So, I would do something like below to manipulate your scenario.
function getNewDate(originalDate, dateOffset){
// converting original date to milliseconds
var originalMS = Date.parse(originalDate);
// calculating your date offest in milliseconds
var offsetMS = dateOffset * 1000 * 3600 * 24;
// apply the offset millisecons to original moment
var newDateMS = originalMS + offsetMS;
// Convert it back to new date object and return it
return new Date(newDateMS);
}
You need to pass a date object and desired timespan in days as parameters to this function. So you would call it as follows
var originalDate = new Date();
// To get new date after 2 weeks for the given original date
getNewDate(originalDate, 14)
// To get new date before 2 weeks for the given original date
getNewDate(originalDate, -14)
I hope this would answer you problem.

Sachintha Lakshan Silva
- 51
- 1
- 3