2

I have time interval e.g. "01:30:00" as the string. Now I want to convert this string to a valid DateTime in javascript to manipulate. for example: add 1 hour.

Morteza QorbanAlizade
  • 1,520
  • 2
  • 19
  • 35
  • This might help you: http://stackoverflow.com/questions/16072056/convert-string-to-time-javascript-hm – Apb May 09 '16 at 05:49

2 Answers2

1

You can use the momentjs library to do this rather easily.

var epoch = moment(str).unix();

http://momentjs.com/

Also refer

http://www.sitepoint.com/beginners-guide-to-javascript-date-and-time/

0

I solved my problem.

first Get current DateTime by new Date() second use .toDateString() third attach my time interval e.g. "01:30:00".

new Date().toDateString() + ' ' + "01:30:00" // Mon May 09 2016 01:30:00

Now use moment.js

var t = moment(new Date().toDateString() + ' ' + "01:30:00");

ّFinaly add for example 1 hour by moment().add()

var finalTime = t.add(60, 'minutes').format("hh:mm");

demo

var stringTimeInterval = "01:30:00";

   var t = moment(new Date().toDateString() + ' ' + stringTimeInterval);

   document.getElementById("demo").innerHTML = t.add(60, 'minutes').format("hh:mm:00");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<div id="demo"></div>
Morteza QorbanAlizade
  • 1,520
  • 2
  • 19
  • 35