Moment js - format seconds in specific way - momentjs

I would like to use moment.js to format seconds with HH:mm:ss format. But if seconds is less than 3600 (1 hour) I would like to only display mm:ss. So, there is a config to set in moment js or I need to use classic if statement

Related

Moment.js toISOString result is different?

Is something wrong with code.
var mom = moment("23-11-2016 00:00", "DD-MM-YYYY HH:mm");
alert(mom.toISOString());
//result 2016-11-22T17:00:00.000Z
Why the result is not 2016-11-23T00:00:00.000Z? How I can get 2016-11-23T00:00:00.000Z result?
As the doc says:
By default, moment parses and displays in local time.
while .toISOString() always returns a timestamp in UTC:
Note that .toISOString() always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString(), as outlined in the ES2015 specification.
You probably have -7 hours offset from UTC.
Use format() if you want to display date in local time.
If your input string represents a UTC time, then use moment.utc(String, String);

how to get default timezone time in moment timezone

I am using moment timezone to set default time
$moment.tz.setDefault('Europe/London');
$moment().toString();
is providing me the time in the defined timezone. but I need the timestamp. so I am using
$moment().now();
but it's returning the timestamp in local timezone. how can I get the timestamp of the default timezone(Europe/London)?
Moment.now is not really a public api. It is intended to be used for testing purposes only. All that you want to do is format a moment, so your code would be as follows:
moment.tz.setDefault('Europe/London');
moment().format(); //"2016-10-18T21:57:39+01:00"

impossible to convert a date more than three weeks with momentjs

I am trying to use the plugin js calendarfull initialized and I would like the calendar.
and then stored in the db.
as I receive a date in FR, at first I call into IN with momentjs like this:
moment(new Date("01/05/2015")).format("YYYY-DD-MM")
it returns me well : 2015-01-05
Now if I try to change this date : 30/05/2015
to put it in EN told me the date is invalid
by cons if I use : 11/05/2015
he converted me well the date in EN
I feel that we are limited in the conversion of the dates from the time lag of the current date, since it is impossible to convert the date Date 2015-01-06ditla me it is invalid .
there would have a way to automatically convert the date momentsjs even a date more than 3 months?
Thank you in advance.
Don't create the moment instance from a string without specifying the format. It will behave differently on each browser. Instead, use the String + Format constructor:
moment("01/05/2015", "DD/MM/YYYY").format("YYYY-DD-MM");
// => '2015-01-05'
moment("30/05/2015", "DD/MM/YYYY").format("YYYY-DD-MM");
// => '2015-30-05'
From moment.js docs:
Warning: Browser support for parsing strings is inconsistent. Because
there is no specification on which formats should be supported, what
works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings,
you should use String + Format.

MomentJS toISOString with local timezone

I've been using toISOString to serialize momentJS date before sending to the server via jQuery. It works fine for me except it converts date to UTC but I need to keep the local timezone. Does momentJS have a method for this?
You can either call .format() without any parameters, or you can call .toISOString(true). Both are in ISO 8601 extended format, the difference is only in whether milliseconds are included or not.
// get the current moment, in local mode
const m = moment();
// format without parameters will give the ISO string including offset
console.log(`moment().format() === "${ m.format() }"`);
// if you want to include milliseconds, you can use toISOString(true) instead
console.log(`moment().toISOString(true) === "${ m.toISOString(true) }"`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Added in 2.20.0:
moment().toISOString(true)
Output:
"2018-05-13T16:16:13.507+03:00"
Found answer here.
Documentation.

Rails datetime input and microseconds

I have a model with that has a datetime to store both time and date. I am using a calendar that support both, problem is that rails won't update the form sometimes:.
For example, setting ste start_date = 01/14/2011 12:00
Does not work
By contrast start_date=01/11/2011 00:00
Nothing else changes
So the problem is I was sending mm/dd/yy and rails uses dd/mm/yy
I fixed using JQuery
$('#start_datetimepicker').datetimepicker({ dateFormat: 'dd-mm-yy' });
$('#end_datetimepicker').datetimepicker({ dateFormat: 'dd-mm-yy' });
Date.parse uses a heuristic algorithm to guess about how it should parse dates.
Generally, using slashes like "01/11/2010" triggers it to parse as "MM/DD/YYYY" while using dashes "01-11-2010" triggers it to parse as "DD-MM-YYYY". So your input above needs to either pass dashes instead of slashes, or switch the order of the day and the month.

Resources