How we can add minutes to time, I want to add:
time = 21:36:13 and minutes 21:33
and want to get result 21:57:46
A JavaScript Date object stores time as the number of milliseconds since 1970/01/01 00:00:00 (in what should be UTC if the rest of your application is written properly). To add minutes and seconds, simply multiply te values to get the equivalent number of milliseconds, something like this: newDate = new Date(oldDate.getTime() + (((minutesToAdd * 60) + secondsToAdd) * 1000))
You should think about what you expect to happen during daylight saving time transitions. If the application is designed properly, the value in the Date object will be UTC, so the calculation above will always work correctly, but obviously the displayed value will be formatted as local time.
You may find a library such as Datejs useful.
What are you using to represent time? if you are using a native Date object, you can do something like this:
var addTime = function (baseDate, hours, minutes, seconds) {
return new Date(baseDate.getTime() + hours*3600000 + minutes*60000 + seconds*1000);
}
This is basically creating a new Date object adding a series of hours, minutes and seconds to the base Date provided (all of it in milliseconds). Here's the reference to work with Date objects.
You can use Date object with only taking interest in time. Here is your example:
function Foo()
{
time = new Date();
time.setHours(21);
time.setMinutes(36);
time.setSeconds(13);
time.setMinutes(time.getMinutes() + 21);
time.setSeconds(time.getSeconds() + 33);
alert(time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds());
}
Hope it helps :D
Related
I am trying to convert string values expressed in hours and minutes -- for example, the values I'm working with are strings like so: "1:30" or "00:45" -- into minutes in JavaScript. I've seen plenty of examples going the other way, but haven't found any solutions for this particular question, where you go from a string value in this "0.00" string format to minutes.
Use a Date() object and the getMinutes() function. Since you only care about a time, you'll need to initialize the object with a dummy date...
var time = "01:45";
var d = new Date("2000-01-01 " + time + ":00");
var minutes = d.getMinutes() + (d.getHours() * 60);
console.log(minutes.toString());
I read timestamp from MySQL db (is type timestamp).
Like this: 2017-04-01 15:34:31
I want to format this date using jquery and set up to some span element.
I am new in jquery and I don't know how to do it.
Thank you.
Try the DATE_FORMAT(date,format) function in MySQL. Something like:
DATE_FORMAT(NOW(),'%m-%d-%Y')
would give you 04-01-2017. Then wrap that in a <span>.
If you are using PHP then
strftime('%m-%d-%Y',$timestamp);
is an alternative. there are plenty of examples in Stack Overflow; Google will get you there.
If you would are receving the timestamp on the client side you can Convert a Unix timestamp to time in JavaScript
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
To add the formatted text to a span you could use javascript or jQuery
// Javascript
document.querySelector("span").text(formattedTime);
// jQuery
$("span").text(formattedTime);
For more information regarding the Date object, please refer to MDN or the ECMAScript 5 specification.
Additionally:
If you must use jQuery instead of Javascript, consider a plugin
https://github.com/phstc/jquery-dateFormat
There is Javascript library https://momentjs.com/ for time
There is a great JS talk about time https://www.youtube.com/watch?v=2BdFg5JT9lg
Date(Date.parse(data.timestamp[i]))
This works assuming you are iterating through timestamps sent to the frontend from a Mysql query, which returns arrays. You could also write it like:
Date(Date.parse("2020-01-08 06:36:59"))
How do I use a different time zone time in a JavaScript?
I already know how to get the current system time but I am confused how to display different time zone time. This one is displaying as an alert, but I need it as a usual displayer not as an alert.
function calcTime(city, offset) {
d = new Date();
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
nd = new Date(utc + (3600000*offset));
return "The local time in " + city + " is " + nd.toLocaleString();
}
alert(calcTime('U.S.A', '-5.0'));
The "time" is absolute.
How you display the time (including time zone) is representational.
So the key thing to understand is that you DON'T want to modify the time VALUE - you just need to alter your FORMAT statement to use one or another time zone.
For example, this uses the Moment.js timezone plugin:
moment().tz("America/Los_Angeles").format("h:mm a")
Here is a great reference:
Stack Overflow 'Timezone' tag wiki
I've got a variable that holds a number of miliseconds which represents the timespan from now to a specified point in the future.
I want to convert this milisecond figure into a timespan value I can display to users.
I know I can do this the naive way with modulo arithmetic and manually displaying the result to the user, but I want to do this using the Date() API built-in to Javascript/ECMAScript.
This is how I generate it:
var timespanInMS = timeInFuture.getTime() - now.getTime();
var diff = new Date( timespanInMS );
alert( "Hours: " + diff.getHours() + " Minutes: " + diff.getMinutes() );
However this only works when the user's computer is in the UTC timezone. If they're in Pacific (UTC-8) then the value of 'diff' is off by 16 hours (even though the timespanInMS figure is the same).
Thanks
Perhaps getUTCHours and getUTCMinutes might work? See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date for more information.
Hi I'm passing a unixtimestamp to a javascript IF statement, can anyone tell me how to generate a unixtimestamp one minute in the future with javascript.
Anyhelp would be helpful.
Thanks
The JavaScript Date object has a getTime() method that returns milliseconds since 1970. To make this look like a UNIX timestamp, you need to divide by 1000 and round (with Math.floor()). Adding 60 get's your one minute ahead.
var d = new Date();
var unixtimeAdd60 = Math.floor(d.getTime()/1000)+60;
UNIX time is just the number of seconds since 1970-01-01Z. So just add 60 you'll get a timestamp one minute later.
JavaScript Date object's getTime returns the number of milliseconds since midnight Jan 1, 1970.
Try this.
var oneMinLater = new Date().getTime() + 60 * 1000;
var d = new Date();
d.setTime(oneMinLater);
Another way to get the unix timestamp (this is time in seconds from 1/1/1970) in a simple way its:
var myDate = new Date();
console.log(+myDate + 60); // you just sum the seconds that you want
// +myDateObject give you the unix from that date