This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
I am new to Node Js
I am using Date.now() to get the timestamp of current time
But i wanted the timestamp value of only current date not including the time.
For Example
Now current time is August 9, 2017 8:19:28 PM and the timestamp will be 1502309968000
But I want timestamp of only August 9, 2017 12:00:00 AM and the timestamp will be 1502236800000
How to solve this ?
Create a date object, and set hours, minutes, seconds and milliseconds to zero
var date = new Date();
date.setHours(0,0,0,0);
var time = date.getTime();
console.log(time);
Note that setHours accepts minutes, seconds etc. as well.
.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
just set the Hours to 0
d = new Date();
d.setHours(0,0,0,0);
Try http://momentjs.com/
It will give you the format that you needed.
enter image description here
Related
This question already has answers here:
How can I get seconds since epoch in Javascript?
(10 answers)
Closed 2 years ago.
I am attempting to use the Google Maps timezone API to get the datetime for a specific location (e.g. New York).
According to the API documentation:
Required Parameters:
timestamp specifies the desired time as seconds since midnight, January 1, 1970 UTC.
The issue I am having is calculating the difference in seconds from two Date() objects:
let currentDate = new Date(); // local user time
let epochDate = new Date('January 1, 1970'); // epoch date
// calculate seconds since (currentDate - epochDate)
Use the getTime() function on a Date instance:
let secondsSinceEpoch = new Date().getTime() / 1000;
No need to set Epoch (January 1, 1970), all dates are relative to that date.
This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Closed 4 years ago.
I have been trying to calculate the exact time since a very specific date in history using Javascript.
The Date is Feb 24th 2008 17:30 GMT+0
I need help in calculating exact time passed down to the second using Javascript.
Here is the previous date and the current date.
I need help in calculating Hours, Minutes and Seconds since that date/time.
var previousDate = new Date("Sun Feb 24 2008 17:30:00 GMT+0");
var currentDate = new Date();
It's easy to calculate the milliseconds between two dates:
var millis = currentDate - previousDate;
From there you can calculate the seconds:
var seconds = Math.round(millis / 1000);
Calculation of minutes, hours, ... is straightforward (division by 60 or 60*60).
Parsing of date strings in javascript fraught. If you have a specific date, far better to avoid the built–in parser. If it's UTC, use Date.UTC to generate the time value.
Then just subtract from any other date to get the difference in milliseconds and convert to seconds, as hgoebi suggests.
var epoch = new Date(Date.UTC(2008,1,24,17,30));
console.log(epoch.toISOString());
console.log(`Seconds from epoch to now: ${(Date.now() - epoch)/1000|0}`);
This question already has answers here:
Extract time from moment js object
(4 answers)
Closed 4 years ago.
I have this date and time format 1/1/2000 20:00:00 and would like to only get the time in this form 20:00 PM or 08:00 AM
How can I format it as hh:mm a to my date?
I get it this way:
var momentDate = moment($scope.workshop.hour, 'YYYY-MM-DDTHH:mm:ss');
var jsDate = momentDate.toDate();
jsDate.toLocaleString();
Try format with HH:mm A as an argument. The A argument will display capital AM or PM as appropriate. Here's an example to get the current time into that format.
var date = new Date()
var momentDate = moment(date);
momentDate.format('HH:mm A')
Updated
Updated to match OP's request.
This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed 4 years ago.
I have From-Date, To-Date, and Total-Days...if i have a From-Date and Total-Days then i have to calculate To-Date automatically
I Used The Following method to get the To-Date day
var FromDate = $("#FromDate").val();
var TotalDays = $("#txtDays").val();
FromDate.setDate(parseInt(FromDate.getDate()) + parseInt(NoOfDays));
var dd = FromDate.getDate()-1;
This Will Not Work For Day 1 of every month.....Since It Returns 0
How To Handle This Situation or help me to solve this in another way....Thanx In Advance
try this
var d = new Date('08-20-2018');
d.setDate(d.getDate() - 1);
alert(d.getDate());
Date object is smart enough to know what to do if you set any of the "components" (month, day, hour, minute, second, millisecond) outside of "normal" range - it does the maths for you
If you only need to know the previous date, you can always subtract the 24hours from the epoch time and then convert it back to date object and get the date like:
new Date(new Date('08-01-2018').getTime() - 24*3600000).getDate()
new Date('08-01-2018').getTime() will give you the epoch time of the date you want previous date from
24*3600000 is subtracting the 24 milliseconds from the epoch
After subtracting the value you get the previous day epoch and using the new Date() constructor you are getting the Date object again
On this new date object you can call getDate() method to get the correct result of 31.
I have given date to Date constructor in MM-DD-YYYY format
This question already has answers here:
How do I get a UTC Timestamp in JavaScript?
(16 answers)
Closed 10 years ago.
How can I get the current UTC timestamp in JavaScript? I want to do this so I can send timestamps from the client-side that are independent of their timezone.
new Date().getTime();
For more information, see #James McMahon's answer.
As wizzard pointed out, the correct method is,
new Date().getTime();
or under Javascript 1.5, just
Date.now();
From the documentation,
The value returned by the getTime method is the number of milliseconds
since 1 January 1970 00:00:00 UTC.
If you wanted to make a time stamp without milliseconds you can use,
Math.floor(Date.now() / 1000);
I wanted to make this an answer so the correct method is more visible.
You can compare ExpExc's and Narendra Yadala's results to the method above at http://jsfiddle.net/JamesFM/bxEJd/, and verify with http://www.unixtimestamp.com/ or by running date +%s on a Unix terminal.
You can use Date.UTC method to get the time stamp at the UTC timezone.
Usage:
var now = new Date;
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() ,
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
Live demo here http://jsfiddle.net/naryad/uU7FH/1/
"... that are independent of their timezone"
var timezone = d.getTimezoneOffset() // difference in minutes from GMT