Formatting and pretty printing dates with jquery - javascript

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.

Related

moment - Display different format based on condition

I'm using momentjs and need to display date in the following way:
If recent (within last 7 days):
Tuesday 9:40am
If not recent (7 or more days ago):
Jun 15
It looks like a common use case to me, but I wasn't able to find docs for it. How can I accomplish this? Preferably without additional JavaScript logic.
Use their isBetween method.
Check out my fiddle.

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

difference between moment js functions and javascript date functions [closed]

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

Chronic for 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

Need help parsing date and time in javascript!

I'm adding a facebook feed to a jquery mobile application i am working on. I am trying to parse the "created_time" that is in the JSON data. This is what is in the returned JSON: 2011-01-29T16:30:03+0000. I would really love to have the data returned and displayed the same way that facebook has it. That would mean that if the post was less than an hour ago it would display as ## minutes ago, if it was more than an hour ago but less than a day ago it would display as ## hours ago, otherwise it would display as February 2 at 6:54pm. I would love some help with this! Thanks a lot!
Try pretty date from the author of jQuery. Available standalone or as a jQuery plugin.
By default it doesn't format dates older than a month, so you may want to edit this to ignore after X hours/days rather than months.

Categories

Resources