How to change time with moment. Js?

I want to change for a while using moment.js.

I have the next time:, Tue May 16 2017 15:34:23 GMT+0300 (FLE Daylight Time)

and I want, for example, to change it to 11.11

.

And there must be time Tue May 16 2017 11:11:23 GMT+0300 (FLE Daylight Time)

.

How can I implement this?

+6
javascript date time typescript momentjs


source to share


1 answer


As pointed out by others in the comment, you must:

  • Parse your input as a moment object, you can use:
    • moment(String, String)

      if your input is a string
    • moment(Date)

      if your input is a JavaScript date
  • Use moment setters (for example set

    ) to set hours and minutes.

You can use format()

to display a momentary object. If you need to convert a moment object to a JavaScript date, you can use the toDate()

.



Here's a live sample:

var dateString = 'Tue May 16 2017 15:34:23 GMT+0300 (FLE Daylight Time)';
var m = moment(dateString, 'ddd MMM D YYYY HH:mm:ss ZZ');
// Use moment(Date) if your input is a JS Date
//var m = moment(date);
m.set({h: 11, m: 11});
console.log(m.format());
console.log(m.toDate().toString());
      

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
      

Run codeHide result


+11


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics