16

I have a unix timestamp: 1368435600. And a duration in minutes: 75 for example.

Using javascript I need to:

  1. Convert the timestamp to a string format hours:mins (09:00)
  2. Add n minutes to the timestamp: timestamp + 75mins

I tried the moment.js library:

end_time = moment(start_time).add('m', booking_service_duration);

booking_service_duration was 75 but it added an hour. I'd also rather not have to use another js library

iamjonesy
  • 24,732
  • 40
  • 139
  • 206

3 Answers3

18

To add 75 minutes, just multiply by 60 to get the number of seconds, and add that to the timestamp:

timestamp += 75 * 60

To convert to hours:mins you will have to do a bit more math:

var hours = Math.floor(timestamp/60/60),
    mins = Math.floor((timestamp - hours * 60 * 60) / 60),
    output = hours%24+":"+mins;
nullability
  • 10,545
  • 3
  • 45
  • 63
  • thanks, the addition works but the convertion isn't quite right - http://jsfiddle.net/Z5986/ - outputs: 380121:0 – iamjonesy Apr 27 '13 at 08:38
  • My mistake. It was 380121 hours since the unix epoch but that probably isn't very useful information. I've updated the answer to output the correct time in 24h format. – nullability Apr 29 '13 at 15:32
5

Unix time is the number of seconds that have elapsed since 1 January 1970 UTC.
To move that time forward you simply add the number of seconds.

So once you have the minutes, the new timestamp is oldTime + 60*minutes
For the conversion look up parsing libraries, there is code out there for this, do some research.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
1

So you want to convert a timestamp you have, timestamp, to locale time string after adding some time interval, specifically minutes, to it.

Whether you have a kind of date-time string or a kind of epoch mili/seconds, just create a Date object:

const date = new Date(timestamp);

Keep in mind since what you need to do require to add/substract some numbers (your case: minutes) to another number, not some date object or some date-time string, and that number is the epoch mili/secods of your date. So, always you will need the number representation of your date in mili/seconds. JavaScript Date.prototype.getTime() does return epoch miliseconds of your date. Use it:

const miliseconds = date.getTime();

Add as many as miliseconds to it:

const newMiliseconds = miliseconds + (75 * 60 * 1000);

After that, as you said you need a date-time string, well a portion of it; locale time string, you will need to go all the way back; from numbers to date object and to a date-time string:

const newDate = new Date(newMiliseconds);
const newTimestamp = newDate.toString();

Or instead of getting the whole string of it, use the following specialized method to get the format/portion of the string representation of the date object that you like directly:

const newTimestamp = newDate.toLocaleTimeString(); // "12:41:43"

Finally, all you have to do is to just strip the last semicolon and seconds to get hours:minutes format:

const newHoursMins = newTimestamp.slice(0, -3);

Better make a function of it:

function timestampPlus(timestamp, milisecondsDifference, toStringFunc = Date.prototype.toString) {
  const date = new Date(timestamp);
  const miliseconds = date.getTime();
  const newMiliseconds = miliseconds + milisecondsDifference;
  const newDate = new Date(newMiliseconds);
  const newTimestamp = toStringFunc.call(newDate); // a bit advanced stuff here to let you define once and use whatever kind to string method you want to use, defaults to toString()
  return newTimestamp;
}

I left the final formatting out here. You can use this for substraction as well by pasing a negative second argument. Note the seconds argument is in miliseconds and unix timestamp varies and might given to you as seconds instead, in which case you will need to convert it to miliseconds or change the above funciton definition.

function timestampPlus(timestamp, milisecondsDifference, toStringFunc = Date.prototype.toString) {
      const date = new Date(timestamp);
      const miliseconds = date.getTime();
      const newMiliseconds = miliseconds + milisecondsDifference;
      const newDate = new Date(newMiliseconds);
      const newTimestamp = toStringFunc.call(newDate); // a bit advanced stuff here to let you define once and use whatever kind to string method you want to use, defaults to toString()
      return newTimestamp;
    }
console.log("new Date(1368435600*1000).toLocaleTimeString(): ", new Date(1368435600*1000).toLocaleTimeString())
console.log("timestampPlus(1368435600*1000, 75*60*1000, Date.prototype.toLocaleString): ", timestampPlus(1368435600*1000, 75*60*1000, Date.prototype.toLocaleTimeString))

Apart from what you need, for last parameter, toStringFunc, your options vary and encompasses all related Date methods, the are on Date.prototype:

  1. toString
  2. toDateString
  3. toTimeString
  4. toLocaleString
  5. toLocaleDateString
  6. toLocaleTimeString
  7. toIsoString
  8. toUTCString
  9. toGMTString
  10. toJSON
sçuçu
  • 2,960
  • 2
  • 33
  • 60