This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 3 years ago.
I need to get the date ( day of the month ) from UNIX epoch time .
I can get the date from this:
new Date ().getDate()
But I need to get from Date.now(), Date.now().getDate() gives me an error..
How to get that date ( day of the month ) ??
You can pass number of miliseconds to Date constructor like:
let now = Date.now();
let date = new Date(now).getDate();
console.log(date);
Related
This question already has answers here:
Convert UTC date time to local date time
(38 answers)
Closed 3 years ago.
I'm trying to convert UTC time 7:27:02 AM to the local timezone. Converting just HH:MM:SS AM. Currently, I'm in GMT05:30.
Here, first, on lines 1 and 2, you get the current date and the current time zone offset, which is the difference in minutes (which you have to convert to milliseconds) between the UTC time and the local time. Then you'll get the timestamp from the date that you want, which is 7:27:02 AM (for PM you'll have to sum 12 hours), and subtract the offset. Then you just have to convert the timestamp to Date Object.
var date = new Date();
var offset=new Date().getTimezoneOffset()*60000;
var date2 = new Date(date.getFullYear(),date.getMonth(),date.getDay(),7,27,2).getTime()-offset
date2=new Date(date2)
console.log(date2)
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:
How do I get a timestamp in JavaScript?
(43 answers)
How to set Hours,minutes,seconds to Date which is in GMT
(3 answers)
Closed 5 years ago.
I'm having a time in this format like
var time = "22:00:00"
I need to convert this into UTC time format like 1567890764
I'm using this one to convert time into UTC.
Math.floor(new Date().getTime() / 1000) //it return current UTC timestamp
how to pass the "time" variable and get UTC timestamp for that particular time?
You can just create UTC Timestamps with the full date.
You can use the .getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds.
let datestring = "2017-10-21 13:22:01";
let date = new Date(datestring);
console.log(date.getTime() / 1000); // will return 1508584921
You can also take a look at Moment.js which is great for handling Date and Time in Javascript.
EDIT:
If you want todays date 22:00:00 just init new Date and set the time like this:
let date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);
This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);
This question already has answers here:
convert iso date to milliseconds in javascript
(10 answers)
Closed 7 years ago.
I have a date as a String like : 2015-12 for december 2015.
I would like to convert this date to get this date in milliseconds in javascript. (From the first day of the month)
How can i do it ?
Try using the Date object (new Date()).
You could do :
YourDate + "-01"
And then try to convert to a date
d = new Date(YourDate)
And finaly :
d.getMilliseconds()