How to create a timestamp from date and time - javascript

i have a datepicker and some time slots in my view. the time slots are checkboxes and the value of checkbox should be a timestamp which i aim to get by combining the value from datepicker and the checkbox slot that was checked by the user. Is there any way to do this using javascript of jquery.
Example:
$('#inline_datepicker').datepicker("getDate") + "02:30PM" => create timestamp

I'm not clear on exactly what you're looking for here, but to just get a basic timestamp in string format using milliseconds (from Jan. 1, 1970 at midnight, as is the standard) you can just do this using vanilla javascript:
var timestamp = new Date(2014,1,3,18).getTime();
The format is yyyy/mm/dd/hh and it is zero based and the hours use a 24 hour clock, also called military time if you are from the United States. The above example gives the timestamp for February 4, 2014 at 6pm.
You can then always add time to the time stamp by adding time to that value, too. 1 day is 24 hours, one hour is 60 minutes, one minute is 60 seconds, and one second is 1000 milliseconds. That means after creating a timestamp, if you wanted to add 4 days, 13 hours, 49 minutes, and 6 seconds, you could do this:
var timestamp = new Date(2014,1,3,18).getTime();
timestamp += (((4*24+13)*60+49)*60+6)*1000;
Finally, you can always convert back to a human-readable timestamp using the .toUTCString() method in conjunction with creating a new Date object. The final code would read as such:
var timestamp= new Date(2014,1,3,18).getTime();
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 04 Feb 2014 18:00:00 GMT" */
timestamp += (((4*24+13)*60+49)*60+6)*1000;
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 09 Feb 2014 07:49:06 GMT" */

Try
var date = $('#inline_datepicker').datepicker("getDate");
//24 hour clock
date.setHours(14, 30);
console.log(date)
Demo: Fiddle
To get the time in milliseconds use date.getTime()

Related

How to get a date time object in UTC that is the last time it was a certain time

I need to make sure if its after 21 hours in UTC or 4 pm est to set the initial day in the date picker as the next day although i need help with the logic of getting a date time object of the last time when it was last 21 hours in UTC.
For example if its 22 hours in utc the last time it was 21 hours was a hour ago so i just use the same utc date and subtract a hour
although a couple hours after it will be a new utc date how do i represent that? Does any one have any ideas?
Im using datetime and using custom color picker libraries is not the solution i'm interested in. I just need help on the logic of how to set Initial date/ Current date in my custom calendar picker with the criteria that after 21 hours utc it should move to the next day.
You just need to test the UTC hour, which is returned by getUTCHours, so:
function getDate(date = new Date()) {
// Copy date so don't affect original
let d = new Date(date);
// If 2100 UTC or later, set to tomorrow UTC
if (d.getUTCHours() > 20) {
d.setUTCDate(d.getUTCDate() + 1);
}
// Set the time to 2100
d.setUTCHours(21,0,0,0);
return d;
}
// Current value
console.log(getDate());
// Value for 2021-03-01T22:00:00Z
console.log(getDate(new Date(Date.UTC(2021,2,1,22)))); // T21:00:00.000Z

UTC time from new Date() using JS

I have checked so many places but could not find a proper answer. I have to get current time in UTC and then subtract few days (say 2 days). So if today is 25th March, I would like to get data of past 2 days starting from 23rd March 00:00 hours GMT.
I can get individual hours and minutes in GMT. But the moment I do new Date() it gives me in local timestamp. Normally I would subtract the current GMT hours and minutes from current time and the number of days I have to subtract.
But when I do a new Date() instead of GMT I get local time. I can do toUTCString() but that is after getting the time in my local time format. If I subtract my local time too then my code won't work universally.
So I need to get the new Date() function in UTC format. I checked a lot of places but nothing seems to work.
The getTime method will return a number representing the milliseconds elapsed from the epoch (1 January 1970 00:00:00 UTC). This method always uses UTC for time representation.
Then you could subtract 2 days and get another Date instance:
var timestamp = new Date().getTime();
timestamp = timestamp - 2 * 24 * 60 * 60 * 1000;
var newDate = new Date(timestamp);
I recommend you use Moment.js to operate with Dates.
In particular adding or subtracting dates is very easy using this library, for example:
moment().subtract(2, 'days')
If you want to do it natively, there are many ways to do it, but one of them is this:
const d = new Date();
d.setDate(d.getDate() - 2);
About UTC date:
But the moment I do new Date() it gives me in local timestamp
That's just a string representation, that is different depending on the environment. Dates are stored internally in UTC, so there is no problem using new Date() and operating with it.

How to only change the hour and seconds portion of a utc timestamp?

I've got a UTC timestamp in milliseconds.
It represents 16:00 on a certain day in GMT time.
timestamp: 1450281600000
I want to modify only the hours, minutes component portion of this value and return the new value.
For example 16:30 is 59400000 but it doesn't have the days and year.
How do I correctly change the utc stamp?
I'm programming in Javascript.
Throw your timestamp into a Date object, manipulate it with the date functions, and then use valueOf to return a timestamp again.
var d = new Date(1450281600000);
d.setHours(1);
d.setMinutes(30);
alert(d.valueOf()); // 1450247400000
You'd want to use standard Date object.
For example, to change 16:00 to 16:30 of that day, you'd do like this:
dt = new Date(1450281600000); // instatiates Date from timestamp
// Wed Dec 16 2015 17:00:00 GMT+0100 (CET) in my local representation
dt.getMinutes(); // will return 0
dt.setMinutes(30);
// dt now is represented as 1450283400000 timestamp...
dt.getTime(); // ...which you can see here.

How to calculate the time difference?

How can i do the calculation to get the time difference. I have both the time of the server and the time of the client system in isoDate date format. Here are the output data i have :
Server time which is in UTC converted to local sytem timezone time :
Tue May 22 2012 14:29:51 GMT+0530 (India Standard Time)
Local System Time :
Tue May 22 2012 14:31:51 GMT+0530 (India Standard Time)
I want to do the calculation in such a way that if the local system time is less then or greater then say 5 min (configurable value) ignoring the difference in seconds compared to what the server time is showing want to perform some task else another task. I dont know how to do the calculation because when it is midnight 12:01 am according to the server time then the date, hour and minute changes whereas if the local system is just 4 min behind how to determine the difference is not less then 5 min although the date changed. My main idea is to check everything to match date, time and check the difference. Please help.
function getDateDiff(date1, date2) {
var second = 1000,
minute = second * 60,
date1 = new Date(date1).getTime();
date2 = (date2 == 'now') ? new Date().getTime() : new Date(date2).getTime(); // now means current date
var timediff = date2 - date1;
if (isNaN(timediff)) return NaN;
return Math.floor(timediff / minute);
}
Use:
getDateDiff(YOUR_SERVER_DATE, YOUR_CLIENT_DATE); // output will be in unit minute
or
getDateDiff(YOUR_SERVER_DATE, 'now'); // for current date

Date subtraction in JavaScript

I have two text boxes which accept Start Date and End Date respectively, in format YYYY/MM/DD.
I need to alert the user if he selects an end date that exceeds the start date by 50 days.
Here's what I have so far:
var startDate = new Date(document.getElementsByName('MYSTARTDATE').value);
var endDate = new Date(document.getElementsByName('MYENDDATE').value);
if ((endDate - startDate) > 50)
{
alert('End date exceeds specification');
return false;
}
Just as an example, when I select Start Date as 2012/01/22 and End Date as 2012/02/29
startDate = 'Sun Jan 22 00:00:00 UTC +0530 2012'
endDate = 'Wed Feb 29 00:00:00 UTC +0530 2012'
And the result for endDate - startDate is 3283200000, instead of 38.What am I doing wrong?
3283200000 is 38 days in milliseconds.
38 days x 24 hours x 60 minutes x 60 seconds x 1000 milliseconds
Also, there are 38 days between those two dates, not 39.
An easy solution is to have a variable (constant really) defined as the number of milliseconds in a day:
var days = 24*60*60*1000;
And use that variable as a "unit" in your comparison:
if ((endDate - startDate) > 50*days) {
...
}
The solution by Jeff B is incorrect, because of daylight savings time. If the the startDate is, say, on October 31, and the endDate is on December 20, the amount of time that has actually elapsed between those two times is 50 days plus 1 hour, not 50 days. This is especially bad if your program doesn't care about the time of day and sets all times at midnight - in that case, all calculations would be off by one for the entire winter.
Part of the correct way to subtract dates is to instantiate a new Date object for each of the times, and then use an Array with the number of days in each month to compute how many days have actually passed.
But to make things worse, the dates that Europe changes the clocks are different than the dates that the clocks are changed in New York. Therefore, timezone alone is not sufficient to determine how many days have elapsed; location is also necessary.
Daylight savings time adds significant complexity to date handling. In my website, the function needed to accurately compute the number of days passed is nearly a hundred lines. The actual complexity is dependent on whether the calculations are client side or server side or both and who is viewing the data.

Categories