This question already has answers here:
How to convert milliseconds to date and time?
(3 answers)
Closed 5 years ago.
Suppose to have a future timestamp date.
var timestamp="1511858535000" //Future timestamp
var date=new Date(timestamp);
console.log(JSON.stringify(date));// this line give me Invalid Data
Anyone can help me to understand why?
The timestamp you mentioned in the comments works fine, but you have to pass it into the Date constructor as a number, not a string:
var timestamp = "1511858535000";
var date = new Date(Number(timestamp));
console.log(JSON.stringify(date));
Related
This question already has answers here:
Add A Year To Today's Date
(9 answers)
Closed 3 years ago.
I want to get the timestamp of one-year from now:
var oneYr = new Date();
oneYr.setYear((new Date()).getYear() + 1);
When I try to get the timestamp:
oneYr.getTime() /1000
I get -58351759111000 (not correct)
It's only working for date in the past.
Any idea how to get timestamp for future date?
Use getFullYear instead of getYear.
var oneYr = new Date();
oneYr.setYear((new Date()).getFullYear() + 1);
This question already has answers here:
JavaScript - get the first day of the week from current date
(20 answers)
Closed 4 years ago.
$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);
This question already has answers here:
Convert UNIX to readable date in javascript
(3 answers)
Closed 4 years ago.
JSFiddle: https://jsfiddle.net/u8w3v9fd/1/
I am trying to get the day, month and year from a date that is passed form the database in the format: DD/MM/YYYY. However I can't even seem to get the correct date to show.
Here is my code:
var time = "1522843537";
var regDateOriginal = new Date(time);
var regDate = new Date();
regDate.getMonth(regDateOriginal);
regDate.getHours(regDateOriginal);
regDate.getDate(regDateOriginal);
document.write("<p style='color: #fff'>" + regDate.getDate(regDateOriginal) + "</p>");
As you can see, this is returning:
21
Which is todays date. It should be 4
I have googled it and hacked around with various versions for the past 45 mins. I am a junior and would really appreciated a nicely commented piece of code so I can learn instead of just copying and pasting.
Thank you for your help.
From here
var time = 1522843537;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(time);
console.log(d.getDate());
Of course, you can still do all the other Date functions as needed.
This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 4 years ago.
I want to get the next day's date in Javascript. I can't find anything that will return it like the getDate() function. Thanks!
There is something like that, called setDate().
var date = new Date();
date.setDate(date.getDate() + 1);
console.log(date);
This question already has answers here:
How do I get the current date in JavaScript?
(61 answers)
Closed 8 years ago.
I wanted to know how can I get todays date in as July 17,2014 format. I am new to javascript..
please help
but
I have tried using var today = new Date();..but it gives me current date and not in the format I want.
Try to make use of following method of Date in javascript:
getFullYear(); // 2011
getMonth(); // 0-11, 0-based month in year,
getDate(); // 0-31, 1-based day of month,