Chronic for Javascript? - javascript

I'm looking for something like the Ruby Chronic library, but for Javascript. The main things I need to be able to do is parse natural time entry, not dates. For example:
Typing Would Give
------ ----------
8a 08:00
8:30p 20:30
8:01 AM 08:01
21:22 21:22
noon 12:00
midnight 00:00
I could write it myself, but before I go off and tackle it, I wanted to know if there was something already available.
I'm using jQuery already, so if there's a plugin, that would be hoopy too.
Thanks!

In place of my previous answer recommending Date.js, I'd like to update my answer by recommending Moment.js instead. It's a stable project that is still under active development. In addition, it is far more feature-filled and has a superior API. :)

Date.JS is a wonderful JS library for working with dates and times.

Comparison support for Ruby Chronic.com date and time input formats.
Check this out
http://www.datejs.com/test/ruby_chronic/index.html

Related

Date/Time on Articles <time>

This is a fairly simple question. While I'll certainly accept and appreciate a detailed answer, guidance in the right direction is all I'm looking for as I have no qualms about learning. I still consider myself an amatuer so please forgive me if you find this trivial.
I'm sure you've all seen what I'm looking for here if you've read a blog or any type of news site. Articles usually have some type of heading with "1 Year Ago", "28 Minutes Ago", etc to reflect the difference in time from when an article was published to the current time you are looking at it. What I'm trying to figure out is how that is accomplished?
I learned today that a tag exists but so far I haven't been able to determine how the attributes you can assign to (e.g. datetime="2015-04-27 20:00") it turn into a readable "1 Year Ago". In my head, I'm imagining some ways I might be able to do this with JavaScript but I'm wondering if this is how it's typically done.
Thanks in advance.
What you might have read is that the "special attributes" actually are pseudo-attributes in some front end framework like Angular, React or Vue etc.
In angular they are known as custom directives. Where you can define custom attribute to pass some data into angular code and get some thing back in this case the humanized form of a date.
What you probably want is moment.js and some way to pass the the date into moment.js to parse it if your not using Angular or other frameworks. Since you are nt so descriptive about your code behind I leave it here on how to work that thing out.
A simple example to demonstrate Moment.js Time from now
moment([2007, 0, 29]).fromNow(); // 4 years ago

JS - standalone user-input date interpreter

I've spent literal hours searching the web for a JS library or a Node module that does just one thing: interpret user-input dates of all kinds (just english is fine) and converts them to a standard date.
For example, it should be able to recognize and convert the following into actual dates:
tomorrow
in a year
last week
in 8 hours
in 4 months
8 8 90
65
Sugar.js does this, however I don't want all its other features, and it extends native objects. Moment.js deprecated its ability to do this as they consider it too opinionated - fine.
One would think someone made a library for this. Does anyone know of any?
In cases like this where a library provides a functionality that I need but I do not need the entire library, I will just strip out the portion of the library that I need.
Sugar.js actually provided a customize option, so this is even easier to do.
http://sugarjs.com/customize
Additionally if you use bower or npm for your package management, there is already a package that just has the Date functions from Sugar.js:
https://github.com/fiznool/sugar-date

Parse date(s) out of arbitrary string

So I need to write a module/function that would analyse a string and parse all possible date values given in any arbitrary form, eg:
bla-bla-bla today --> new Date()
january foo Mar 11th bar --> [new Date('2014/1/1'), new Date('2014/3/11')] // more than one date
lorem 11/08 ipsum --> new Date('2014/11/08')
monday --> new Date('2014/10/27')
How difficult would it be? Should I even bother trying, or it's a lot more difficult than I'm imagining? Maybe someone has already done something like that?
Only dates, time doesn't really matter, also I need to do it on the client, crazy eh?
One solution is to use http://momentjs.com/docs/#/plugins/parseformat/
Good luck!
I really depends on how custom you're looking to make it. Your best bet is really to exhaust the current options out there, http://momentjs.com/ or even the thing it "replaced", http://www.datejs.com/. Those libraries aren't meant to do that but you might be able to either contort them to do so or fork them and modify them for your purposes.
If you can't get something custom enough out of those I would recommend going the route of describing the date syntax you want as more of a grammer, then using a parser or parser generator to create something that can correctly parse what you want. Something in the realm of http://pegjs.majda.cz/ or http://marijnhaverbeke.nl/acorn/.
For some context I ran into a similar situation for time only and ended up using Scala's standard parser combinator library to achieve something of this sort. https://github.com/scala/scala-parser-combinators. That only worked out because I was able to express a fairly high level grammar and not get into the weeds of parsing particulars.

Date.js culture info is confusing me

I'm including date.js (from datejs.com) and the en-IE culture info script.
If I call alert(Date.CultureInfo.dateElementOrder) I see the correct dmy, but if I try to interpret the date 02/03/01 I get 3 Jan 2001 and not 2 Feb 2001. I'm not sure whether this is a bug in date.js or a problem with me.
The documentation was confusing me (or I hadn't read it right).
I thought I should include date.js and en-IE.js. In fact, the right thing to do is to include date-en-IE.js, a file I hadn't even noticed existed before.
As usual with datejs, the downloadable file on the front of the site is a year or so out of date, but a live version exists if you dig for it in the svn trunk.

Formatting and pretty printing dates with jquery

I need to display dates in a couple different ways in an app built with jquery.
In some situations, I need the typical "yyyy-mm-dd hh:mma" type of formatting, with all of it's different permutations. In other cases, I need to show dates "pretty printed" similar to how StackOverflow does them:
5 seconds ago
12 minutes ago
3 hours ago
yesterday
2 days ago
My application already uses JQuery UI DatePicker which includes a formatDate() function, but as far as I can tell, there is no way to use it outside of the datepicker. I want to format dates that aren't associated with a datepicker. Is it possible to do this using DatePicker?
The DateJS library can parse dates like "12 minutes ago", but as far as I can tell, it cannot take a Date object and format strings like this. It can format the typical "yyyy-mm-dd" types of formats. This library seems pretty heavy as well.
John Resig's Pretty Dates looks like it can provide the pretty printing ("2 hours ago"), but it doesn't do the standard formatting.
Is there not a single plugin that can do all of this? Is there a way to leverage the DatePicker code so I don't have to load multiple codebases that do the same things?
http://timeago.yarp.com/
Check out prettyDate.
It's made by the same guy that does the jQuery Validation plugin.

Categories

Resources