moment - Display different format based on condition - javascript

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.

Related

MomentJS: Display formatted range between two arbitrary dates

I have two arbitrary dates that I would like to compare using MomentJS. By "arbitrary," I mean that neither of them are now (i.e. moment()).
I would like to display both of these dates as a nicely-formatted string based on the difference between them. For example, if the difference between them is just a few hours, I'd rather display something like "5 am to 8am", rather than "1/1/2018 5:00am to 1/1/2018 8:00am".
Put more succinctly, I'm trying to see if moment can determine which fields are equivalent, and based on this, format the time range as a string without any redundant information.
Is there a way to do this using standard functions in MomentJS?
Here's what I understand so far:
The functions such as toNow(), fromNow(), to(), and from() all provide relative time ranges, but these are in reference to now. What I'm looking for is this same interface, but without referencing now.
I believe that my options are either to:
Use one of the aforementioned functions to compare the two dates, drop the suffix, and display something like "1/1/2018 5:00am + 3 hours", OR
Use a duration to compare the dates, and add custom code to determine equivalent quantities, and set the format accordingly. E.g. if years, months, weeks, and days are equivalent, format each timestamp as "h:mm:ss a".
The output of the second option would be much more desirable. Essentially, I'm trying to implement this option using MomentJS and as little custom code as possible.
It looks like this is handled externally by a plugin called Twix.

I need to make a Google form available for a specific time each day

We have a Google form that we would only like to be made available to users at a specific time each day. It should only be open from 2:00 to 2:15, Monday thru Friday. Wasn't sure if the following would work:
FORM_OPEN_DATE =
FORM_CLOSE_DATE =
Not exactly sure of the value to place after the = sign. Looked the JavaScript Date objects but could not find a one that I thought would work.
There is no built-in functionality to do that. In order to do that you can go with Date API.

Javascript or jquery validating the date of the application for the user to re apply again

I'm currently using dynamics CRM and adxstudio and I was wondering if there's some sort of jquery or javascript code I can use to validate the date of the application. So basically applicants can only re-apply within 4 weeks of their current application expiry date. I provided a screenshot of the list.This is the main list
The applicant is only allow to renew within 4 weeks of the Next Recert Date meaning they cant apply earlier nor later. I already have the id all I need to know is the function or an alert to give the user that they cant recertify yet.
Did you try Moment.js? It is an awesome JS library for date handling.
EDIT: Within the above link, you have many examples of data manipulation. You could use to parse and manipulate dates, like making sure today's date is within the range of [expiryDate - 4 weeks, expiryDate].
Example: moment(expiryDate).subtract(4, 'weeks').calendar();

In Moment.js, why doesn't subtract by 'week' work?

I'm using Moment.js and following line of code doesn't seem to actually do anything:
moment().subtract('week', 1)
In the documentation, it shows an example of subtract being used with 'days' but it doesn't specify that it's the only string you can use. In most of the other functions, it's an option where you can use 'days', 'weeks', 'months', so I'm not sure if I'm doing something wrong or 'weeks' just isn't supported:
Moment.js Subtract Documentation
Here's the example of subtracting days:
moment().subtract('days', 7);
It's also what I ended up using instead of 'weeks' but I'm still curious why 'weeks' aren't supported.
You have it backwards from the Moment API.
moment().subtract(1, 'week');
This is a bit old question but it appears that even back then it was relevant.
Definetely nowadays is even more relevant to consider parse.com moment.js version (1.7.2) to the current moment.js version (2.8.4) and the only API doc that parse.com makes reference to
Check an answer to this in a previous post in Trouble using the Moment module
I would suggest to work always with latest moment.js so you can properly work with the provided documentation (during my search I was not able to find 1.7.2 documentation.. beside you will miss many great features working with the version provided in parse.com.
Add the new version as indicated in the same post in the accepted answer.

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