Date/Time on Articles <time> - javascript

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

Related

How do I add JavaScript to an Object in Unity?

You can skip this: I could do research but a YouTuber Recommended asking a fourm group for help. I guess the reason for this is to get more knowledge then just finding the answer without thinking about any other variables.
Question: I was wondering how I add a Javascript File to my object in Unity. I am new to Unity and the only coding knowledge I have is in JavaScript (Although limited from 1 year of basic programming and 1 year of AP computer Science) not C#.
What I Tried: I have tried dragging the JavaScript File to the object in the hierarchy but that seems to not work. Is there a specific route I should take?

String translation with dynamic text and inputs

I am working on front-end only React application and will soon be implementing internationalization. We only need one additional language... at this point. I would like to do it in a way that is maintainable, where adding a new language would be ideally as close as possible to merely providing a new config object with translations for various strings.
The issue I know we will have, is that we have dynamic inputs inside of sentences as demonstrated below (where [] are inputs and ** is dynamically changing data). This is just an example sentence... lots of other similar type things elsewhere in the app.
I am [23] years old. I was born in [ ______▾]. In 2055 I would be *65* years old.
We could break out 'I am', '*age input', 'years old. I was born in', '*year dropdown'. etc. But depending on the language, word order could be changed or an input could be at the beginning of a sentence etc, and I feel like doing it in that way would make for a really weird looking and hard to maintain language file.
I'm looking to know if there are common patterns and/or libraries we can use to help with this challenge.
A react specific library is react-intl by yahoo. Which is part of a larger project called FormatJS which has many libraries and solutions for internalization. These and their corresponding docs are a good starting point.

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.

Parsing a string into a custom object based on different criteria

As part of a small project I'm working on, I need to be able to parse a string into a custom object, which represents an action, date and a few other properties. The tricky part is that the input string can come in a variety of flavors that all need to be properly parsed.
Input strings may be in the following formats:
Go to work tomorrow at 9am
Wash my car on Monday, at 3 pm
Call the doctor next Tuesday at 10am
Fill out the rebate form in 3 days at 2:30pm
Wake me up every day at 7:00am
And the output object would look something like this:
{
"Action":"Wash my car",
"DateTime":"2011-12-26 3:00PM", // Format is irrelevant at this point
"Recurring":False,
"RecurranceType":""
}
At first I thought of constructing some sort of tree to represent different states (On, In, Every, etc.) with different outcomes and further states (candidate for a state machine, right?). However, the more I thought about this, the more it started looking like a grammar parsing problem. Due to a (limited) number of ways the sentence could be formed, it looks like some sort of grammar parsing algorithm would need to be implemented.
In addition, I'm doing this on the front end, so JavaScript is the language of choice here. Back end will be written in Python and could be used by calling AJAX methods, if necessary, but I'd prefer to keep it all in JavaScript. (To be honest, I don't think the language is a big issue here).
So, am I in way over my head? I have a strong JavaScript background, but nothing beyond school courses when it comes to language design, parsing, etc. Is there a better way to solve this problem? Any suggestions are greatly appreciated.
I don't know a lot about grammar parsing, but something here might help.
My first thought is that your sentence syntax seems to be pretty consistent
1st 3-4 words are generally VERB text NOUN, followed by some form of time. If the total options are limited to what form the sentence can take, you can hard-code some parsing rules.
I also ran across a couple of js grammar parsers that might get you somewhere:
http://jscc.jmksf.com/
http://pegjs.majda.cz/
http://www.corion.net/perl-dev/Javascript-Grammar.html
This is an interesting problem you have. Please update this with your solutions later.

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