I'm starting a new project today. The application is a rewrite of an existing workscheduling application. The application works extensively with Times and TimeRanges.
Time: 08:45, 17:32, ...
TimeRange: from 08:45 till 17:32
I started looking for a library that handles the plumbing for me with this kind of data, eg: Earlier than, later than, in between, etc. I found moment.js and date.js immediately. I feel however that these libraries are more concerned about a certain point in time (a JS Date object) rather than the above described notion of Time and TimeRange.
Does anyone have experience working with Time and TimeRange as I described above in a JavaScript environment? What library could be useful for this?
You can use Date object to compare dates. You can construct 2 dates and then compare using usual operator.
For an ex:
var date1 = new date();
var date2 = new date();
Then compare like date1.getTime() == date2.getTime() or whatever you want to compare.
Hope this will help you.
Have another look at Moment JS, it's probably what you need. It has a rich library of date handling functions, and uses Duration objects to represent time ranges.
http://www.datejs.com and http://momentjs.com are the best as far as I'm concerned. A few more are listed here: http://codegeekz.com/6-javascript-date-libraries-for-developers/
Related
In Javascript I need to work with the concepts of date, time and "date and time " without referring to a particular point in time. This is exactly the same semantics that joda time's LocalDate and LocalTime provide in Java. I've been briefly looking at Date.js and moment.js, but both libraries seem to build on the Date object, which represents a point in time. Is there any javascript library that provides what I need?
Use case:
There is a model entity -a coupon- which has an expiration date (joda time's LocalDate). I want to compare that date with today's date, so I need a representation of today's date (actually a string in yyyy-mm-dd format would do). I know that today's date, and hence the result of the comparison too, will depend on the timezone settings of the browser's but that's not a problem.
I've started a few times on a JavaScript library with similar API to Noda Time / Joda Time / Java 8. I definitely see value in that. However, there's nothing out there as of yet, as far as I know. There are other reasons that make the Date object less than ideal. I'll try to remember to update this post when/if I ever get a new library off the ground, or if I learn of one created by someone else.
In the mean time, the easiest thing would be to use moment.js:
var expDateString = "2015-06-30";
var exp = moment(expDateString, "YYYY-MM-DD");
var now = moment();
if (exp.isAfter(now))
// expired
else
// valid
You could also do this with plain JavaScript, but there are some gotchas with parsing behavior. Moment is easier.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Actually i am thinking about to use moment.js in my new project.because my new project is a employee working sheet application.
I read the moment.js documents.
We can get the current date by using the below code in moment.js
moment().valueOf();
We can get the current date by using javascript
new Date()
The both are giving same result. with same speed (so no performance issue )
also formatting, get methods and set methods are already has javascript. then why i go to moment.js?
Please give me some explanations about the difference's. And let me know which one is best for my new project.
Moment.js
A lightweight JavaScript date library for parsing, validating, manipulating, and formatting dates.
Reason why we use Moment.js - Many web applications today, especially social ones, deal with the concept of time. They are usually employed to sort events or posts, or to mark the moment in which something happens. You can think of your Twitter timeline or your GitHub news feed. JavaScript has a Date object that you can use to manipulate dates, but it often doesn’t have all you need in your web applications
The Moment.js library not only parses dates from strings, but it may also be used to validate, manipulate, and format dates. It supports internationalization, which is so important with dates, as well as human-friendly formatting like "Last Friday at 9:48".
These are all good things for sure, but today's order of the day is date parsing, so let's get into how that works.
Moment.js creates a wrapper for the Date object rather than extend it. To reference the wrapper object, simply call the moment() getter function. The Moment prototype is exposed through the moment.fn property, so you can add your own functions to it if you are so inclined.
Courtesy of
Managing Dates and Times Using Moment.js
A Roundup of Popular JavaScript Date Parsing Libraries: Moment.js
Moment.js is convenient when you want to manipulate Dates.
An Example for moment is given below
moment()
.add(7, 'days')
.subtract(1, 'months')
.year(2009)
.hours(0)
.minutes(0)
.seconds(0);
moment().endOf('day').fromNow();
But if formatting, getting and setting is all you need, you might not need a library for that.
Obviously, I would suggest to go with moment.js. It is one of the most popular Date parsing library.
moment.js has a various inbuilt function's available which is easy to use.
It will save your development time.
Manipulating and parsing dates is very easy.
It comes down to what you're trying to do.
MomentJS provides an API that pretty-much wraps the native Date object, which is why you call .valueOf. new Date is simply giving you the native Date objects (and to manipulate it using moment, you'd need to pass it to its constructor).
I would say general rule of thumb:
If you're creating a date for use on the site or as some kind of UI element, use Moment.
If you're storing a date back to a service layer, use Date.
If you're working in a method that's manipulating the date, use Moment; If that then needs to be passed to another service layer, call .valueOf and save it off.
Moment is great for the UX, but isn't a "standard" (and therefore wouldn't be ideal for serialization/storage).
Moment.js has a good support for adding, subtracting, advanced formatting, finding differences between two days. Also it has internationalization support so you can convert to any language very easily.
just like any library comes with a purpose to aid with something and make it easy to work. momemt.js makes working with dates PAINLESS.
working with dates considering the timezone and formats factors etc is very difficult, but moment.js makes it a breeze to work with dates.
read more here
I'm including the date.js library in my site because I need its functionality.
I have just realized, though, that the standard Javascript parse() method is overwritten by it.
I'm trying to build a line chart in Highcharts, and the data series wants the first element to be be in milliseconds (their demos show them using the Date.UTC() method to achieve this, but my data is returned in a different format).
Short of doing a bunch of string manipulation to put my data into a format that Date.UTC will recognize, is there another way of getting the standard Javascript parse() functionality while date.js is loaded?
I know this isn't a direct solution to your problem, but it may help anyway.
If you want a fully featured date library that doesn't modify the native Date object, I wrote one called Moment.js.
It provides a lot of the things that DateJS provides (formatting, parsing, manipulation, timeago, i18n, etc), but it's smaller, faster, and doesn't ruin the native date prototype.
https://github.com/timrwood/moment
Nope, this is the intended design of date.js. It adds to the "prototype" of the Date object. Some people hate that, some people like it - but you've uncovered one of the drawbacks of this design.
You can tell Highcharts to not use UTC date:
Highcharts.setOptions({
global: {
useUTC: false
}
});
You should do this before you create the chart. Then you won't have to worry about converting your dates to UTC, it will be easier.
after I asked the question, I went ahead and did it this way:
d = Date.parse(data);
y = d.getFullYear();
m = d.getMonth();
d = d.getDate();
dUTC = Date.UTC(y, m, d);
but will now try your suggestions.
I live in Hawaii where there is no Daylight Savings Time. I'm trying to show a world clock that displays dates and times from different time zones, but I also need it to properly calculate the DST offset. The limitation is that I must use the client time, as the application will not be able to get internet access. Is there a jQuery plugin that can handle this, or do I have to write it manually?
Does it HAVE to be a jQuery plugin?
There is a project on github that prototypes the Javascript Date object to be able to handle timezone keys
Regardless of how you do it, you will have to get the tzinfo database to work with your script (well, that's the only way I know of that will give you robust information about DST around the world). I have no personal experience of the library I linked above, but people seem to mention it from time to time, and it is the only pure JavaScript lib I have found to the tzinfo database.
function get_time_zone_offset( ) {
var current_date = new Date( );
var gmt_offset = current_date.getTimezoneOffset( ) / 60;
return gmt_offset;
}
The javascript Date.toLocaleDateString() is silly.
What I need is a function that allows me to simplify the date according to preference.
It would be nice if there were a function which would read the browser date formats (plural) and take an optional parameter telling it which format to use.
I'll explain:
"MM/DD/YYYY" works great for the US and anyone willing to put up with them/us.
"DD/MM/YYYY" is the most common format for people interested in a short simple date format.
"Weekday, Month DayOfMonth, Year" is only useful if you want a super-long and language-dependent output.
I could use this:
var s = "/";
if(locale=='us')
var dateString = Date.getDate()+s+Date.getDay()+s+Date.getFullYear();
else
var dateString = Date.getDay()+s+Date.getDate()+s+Date.getFullYear();
But I'm looking for a more elegant solution that will allow me to store a date mask or format string so people can change the way their dates are displayed according to their own tastes. (Even the super-long language-dependent one if they like it enough.)
Should I re-prototype the Date.toString() method to interpret parameters? Is there a better way?
You can use DateTimeFormat Api.
var now = new Date(0)
console.log(new Intl.DateTimeFormat('en-US').format(now)); //12/31/1969
console.log(new Intl.DateTimeFormat('en-GB').format(now)); //31/12/1969
See this link for Mozilla documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
PROS:
you don't have to add more libraries to you project, increasing the
bundle size.
you don't have to worry about browser support, because almost every browser supports it. https://caniuse.com/#search=intl
PS: if you don't care about bundle size and you want something more "user friendly" and easy to use see moment.js or luxon, they are both great libraries for date operations.
I ran into a very powerful library that takes care of dates and generic formatting:
http://blog.stevenlevithan.com/archives/date-time-format
(Wrong link)
http://jawe.net/wiki/dev/jsdateformat/home
Is pretty powerful and configurable. (It supports java-style formats that I need, such as the "MEDIUM" date format)
Moment appears to be useful and feature-full (just no Medium format): https://github.com/timrwood/moment