How to Handle Durations Without Using Moment.js Durations Feature? - javascript

Up until now, I've been using Moment.js durations feature to handle all my "length of time" needs. As Moment.js is no longer being supported, I would like to move away from it. However, I've used it for so long and now find it difficult to transition. What are other methods I can use to handle durations?

You can try dayjs library or native JS Date objects.

Related

OpenTest Doesn't Take a Standard JS Date for the ReadEmailImap function

I am attempting to pass a value for sentAfter to org.opentest.ReadEmailImap function that contains a string formatted as YYYY:MM:DD HH:DD:MM per this documentation
The closest that I have come to this format is .toISOString(). ref
You can either build the date yourself using the JavaScript API, like here, or you can use a library like Moment.js (The moment.js file can be included in your test as described here). The first method is better for a quick and dirty, one time solution. The second is best if you have more advanced date/time logic that you need to execute.

Daylight Saving Time in Javascript

I am running my web apps in UTC+0530 timezone. I want to find the daylight saving time of UTC+0000 time zone. Is it possible in javascript?
Using javascript only no.
The logic to determine if a country XXX is following DST or no can't be calculated by javascript because it's more ... politic.
See also How to know whether a country is using Daylight Saving Time using JavaScript? which gives a code to theorically calculate what the DST should be.
If you want a native javascript way to do this, and perhaps if this is a one-off requirement, then the SO link by #Emmanuel above is right for you.
If you want to perform different types of date manipulation, I would recommend using the moment.js library.

Does momentjs have something similar to joda-time's localDate? If not, is it best to use .startOf?

In the joda-time library (and I assume Java 8's new time library), you can ignore times and time zones: https://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html
I would prefer to avoid having times factor in in my small app. I just want the user to see their local date. Is there a momentjs equivalent to localdate? If not, would the best workaround be to use .startOf()? Thanks.
Edit: Just to be clear, this is not a formatting question.
"By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc()"
As explained here.
moment().format(); // 2013-02-04T10:35:24-08:00 (local date)
moment.utc().format(); // 2013-02-04T18:35:24+00:00 (UTC date)

Change Date Format from millisecond

I have millisecond and i want to convert it in format Feb-01-2014 09:12:12. I have used following code
var today = new Date(1419359400000);
var p=today.toLocaleFormat('%b-%d-%Y %H:%M:%S');
But it is not working in chrome
Thanks
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat reads:
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Use some date library if you find yourself needing to formats a lot or use the native functions today.getYear(), today.getMonth() etc.
Keep in mind that toLocaleFormat uses the operating system locale which might not actually be what you want. Your dates might appear in a different language than the rest of your application.

jQuery datetime formatter

Is there a working jQuery plugin (or a javascript 'library') for formatting datetimes? I found some, but they were:
not working with hours and minutes (the one from datapicker)
not fully functional - can't give you names of months, leading zeroes, etc.
are just a piece of code written in some blog.
Of course I can implement it, but it'd be better to reuse one. I seek functionality similar to Java's SimpleDateFormat
I've written a JavaScript implementation of the format() method of Java's SimpleDateFormat: http://www.timdown.co.uk/code/simpledateformat.php
The code is a few years old and I'd do it a bit differently now, but it's well tested and works.
Did you try date.js ?
It has a pattern recognition to format dates that is easy to use and has plenty of localisation files available.
ie: Date.today().toString("d-MMM-yyyy HH:mm")
I use http://blog.stevenlevithan.com/archives/date-time-format
Not sure whether it fits all your requirements, but this looks good one:
jquery-dateFormat

Categories