-1

I am doing in accordance with this question and I need to format my resulting time string. I use bootstrap datetimepicker and moment.js and I have this code:

$("#datetimepicker2").on("dp.change", function (e) {
    console.log('date2: ' + e.date.format('MM.DD.YYYY HH:mm:ss') );
    end_date = e.date;
    end_date.toJSON = function(){ return moment(this).format(); };
});
console.log(JSON.stringify(end_date));

My problem is that I receive

"2017-06-20T17:29:05+03:00"

while I need something like this:

"2017-06-20T17:29:05.013Z"

Any ideas how to get it would be welcome. Thank you.

Masha
  • 827
  • 1
  • 10
  • 30
  • Well then go read the moment.js documentation and find out what format specifiers are available …? – CBroe Jun 20 '17 at 14:36

2 Answers2

0

You can convert the moment object to js date and use toISOString() on that. moment().toDate().toISOString();

mihavr
  • 28
  • 1
  • 4
0

You can add .tz() before .format() and pass in the timezone inside .tz("America/New_York"), etc.

$(function() {
  $("#datetimepicker2").datepicker();
  $("#datetimepicker2").on("change", function(e) {
    console.log(moment().utc($(this).val()).format());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js"></script>
Date: <input type="text" id="datetimepicker2" />
Woodrow
  • 2,740
  • 1
  • 14
  • 18
  • does not work, saying `Cannot read property 'toLowerCase' of undefined`. Any ideas what this could be? I am overriding a `toJSON` method in order to preserve timezone – Masha Jun 20 '17 at 14:51