Need help parsing date and time in javascript! - 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.

Related

How to convert time zone from digits/numbers to date openweathermapAPI

I am using openweathermap API, I am trying to fetch the timezone. The api have timezone as: 34200. How can I convert it to date format? I am using javascript.
I am new to it, so I am not sure where to start. I even looked up, but couldn't find any solutions.
It's how much the time has shifted in seconds from the UTC, so 34200 means 9,5 hours to the east. So this one would probably be somewhere in china. If you need help with the code, just say so, I can help you with the code too. But I think this answers your question
You can use moment library for convert time or date. It is very easy to understand and implement.

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

php timezone messing with javascript time

I'm using the TimeAgo plugin to update the time of posts automatically. It makes time like 05-08-2014 15:02:39 to 5 minutes ago.
The problem is, the javascript uses client's clock for time. I'm from Bangladesh, Asia. But in php the default timezone varies from server to server. I'm outputting the format 05-08-2014 15:02:39 in PHP to convert it into something like 5 mintues ago using the TimeAgo plugin.
Because server's timezone is different, instead of showing less than a minute ago on a recent post, it shows nn hours ago. If the server is american, it shows 11 hours ago, if it's indian, the time shows 30 minutes ago. How do I fix it?
As stated on the TimeAgo project page:
Are you concerned about time zone support? Don't be. Timeago handles
this too. As long as your timestamps are in ISO 8601 format and
include a full time zone designator (±hhmm), everything should work
out of the box regardless of the time zone that your visitors live in.
So echo the dates server side with date('c',$timestamp) and everything should work fine.

Parsing String Time Format (NOT Time Zones)

Assuming that time formats (not timezone data, just plain-text Strings representing time in 12-hour clock), what is the best way to parse that data (examples below)?
I feel like this is a pretty simple question, but I've though about it a lot and can't figure out a fool-proof process for parsing out hour and minute values from any one of many time "formats".
I'm using a simple form that allows for entering a time (e.g., the time of a reservation), but there is no way to make the form field (Google Forms, limited customizability) anything other than plain-text which allows for any time format a person wishes to put in there a viable piece of data to work with. Here are some examples of times that can be entered:
1 P // hours would be 13, minutes would be 0
3:15 P.M. // hours would be 15, minutes would be 15
4 P.M // hours would be 16, minutes would be 0
8:30 a // hours would be 8, minutes would be 30
10:45 aM. // hours would be 10, minutes would be 45
12:00 Pm // hours would be 12, minutes would be 0
Those are just some examples. Feel free to fix tags if I'm not in the right topic(s) - I am however using Google Apps Script/Javascript to accomplish this.
I thought about building a couple regex strings to look for certain formats (which I'm sure will take me a while to write since I've never written one before).
I also thought about looking for a ':' character, and an 'a'/'A' character or 'p'/'P' for determing whether or not the +12 hours needs to be added. After that kind of information is retrieved the problem could be solved by truncating the 'p.m.'-type sections, then split(":") to get the correct numbers.
I feel like the first approach is much more stable, but the second solution might be easier. I'm not sure if there is a better way though, those are the two methods my n00b programmer mind could come up with.
I'm glad to hear you have an answer that you think will help you out. But before you write too much code, revisit this assertion you made:
...there is no way to make the form field (Google Forms, limited customizability) anything other than plain-text
Here's how you define a question to present a Date & Time picker, using the Forms UI.
The spreadsheet that is receiving your form responses will end up with a Date object - no need to mess around with strings at all.

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