Date and time without timestamp in Javascript

In Javascript, I need to work with the concepts of date, time, and "date and time" without referring to a specific point in time. This is exactly the same semantics as joda time LocalDate and LocalTime provided in Java. I took a quick look at Date.js and moment.js, but both libraries seem to build a Date object that represents a moment in time. Is there a javascript library that provides what I need?

Use case:

There is a model entity - a coupon that has an expiration date (joda time LocalDate). I want to compare this date with today's date, so I need a representation of today's date (it will actually be a string in the yyyy-mm-dd format). I know that today's date and therefore the comparison result will also depend on the browser timezone settings, but this is not a problem.

+3


source to share


1 answer


I started a few times in a JavaScript library with a similar API to Noda Time / Joda Time / Java 8. I definitely see the value in this. However, as far as I know, nothing has been there yet. There are other reasons that make the object Date

less ideal. I will try not to forget to update this post whenever / if I ever get a new library off the ground, or if I find out about one created by someone else.

At the same time, the easiest is 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 can also do this with regular JavaScript, but there are some bugs when parsing the behavior. The moment is easier.

+2


source







All Articles