Tuomas Toivonen
Tuomas Toivonen

Reputation: 23522

how to convert moment to custom format string in moment.js

I have moment object, and I want to convert it to string with the format dd.mm.yy. What's the best way to do this with moment.js?

I tried toString function, but it returned date in the format of Wed Mar 23 2016 00:00:00 GMT+0200 and it's too much effort to convert it manually.

Upvotes: 1

Views: 13710

Answers (2)

VincenzoC
VincenzoC

Reputation: 31502

You can use moment format to convert moment object to the format you prefer. In your case:

var momObj = moment();
var timeToString = momObj.format('DD.MM.YY');
console.log(timeToString);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

Upvotes: 5

Alexandr Fedoseev
Alexandr Fedoseev

Reputation: 67

moment.js lib the reference to the moment js lib, the only lib you need to work with time!

var momentObj = moment(); // will take now time and date as moment object
var formatDate = "DD.MM.YYYY HH:mm:ss"; // the string that represents desired format.

var result = momentObj.format(formatDate);
//result will be string "04.07.2016 12:25:12"

Upvotes: 1

Related Questions