Tips for displaying timestamps in a user-friendly format, such as: 1 second ago, 1 minute ago, 1 hour ago, 1 month ago

Hey there, I'm a JavaScript newbie seeking some help!

The date I'm receiving from the server is: 2015-01-16T05:55:32.000Z

I want to compare this with the current date and display something like:

1 sec ago

2 min ago

2 hours ago

2 weeks ago

2 days ago

2 months ago

2 years ago

I'm looking to achieve this using JavaScript and AngularJS.

If anyone can assist me with this, I would greatly appreciate it. Thank you in advance.

Answer №1

If you are looking for a powerful JavaScript tool, check out Moment js library. It allows you to perform various tasks easily: http://momentjs.com/

moment("20111031", "YYYYMMDD").fromNow();

With this library, you can get results like x seconds or days ago by parsing the string from your server. It also supports translations in different languages.

For instance, when using the example string:

var readable_date = moment('2015-01-16T05:55:32.000Z').fromNow();

This code provides a clear answer as demonstrated in this fiddle

var date = '2015-01-16T05:55:32.000Z';

var readable_date = moment(date).fromNow();

document.getElementById("date").innerText = readable_date;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.js"></script>

<span id="date"></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resetting form after submitting an image in Meteor

In my Meteor App, I am using CFS for uploading files and everything is working fine except for one issue. When I try to upload another image, the previous uploaded image remains in the form, so I need a way to clear the form after submitting the new image. ...

What is the best way to modify JSON data within a file?

To start, I retrieve a JSON array by using the following JavaScript code: $.getJSON("myJSONfile.json") .done(function(data) { myData = data; }); After storing the data in myData, which is a global variable, I proceed to add more informati ...

Disrupting a Live Stream

Issue: Currently, I am utilizing the stream_get_contents(); function in a PHP file to stream my video while using Flowplayer, following the example shown here. Everything is working smoothly, however, when attempting to load a new video via AJAX, the proce ...

What is the implication when Typescript indicates that there is no overlap between the types 'a' and 'b'?

let choice = Math.random() < 0.5 ? "a" : "b"; if (choice !== "a") { // ... } else if (choice === "b") { This situation will always be false because the values 'a' and 'b' are completely disti ...

Determine the presence of a Facebook user by using JavaScript

I am trying to find a method to verify the existence of a Facebook user based on their ID. As far as I understand, there is a Graph API that can provide a Facebook user's profile picture using their ID in this format: I have noticed that if an inval ...

Managing a collection of input values

Is there a way to effectively manage an array of input fields using reactjs? Take for instance this text field: <input type="text" value="" name="test[]" /> Below is the code I'm currently working with: render () { ...

Uncertainties surrounding the use of JSON data versus a JavaScript object

As a newcomer to programming, I have ventured into Stack Overflow and W3schools to enhance my skills. Recently, I created a small project for educational purposes. One dilemma is bothering me - is the JSON file I generated actually in proper JSON format o ...

A guide on how to pass an object as a parameter when opening a new view in JavaScript using the MVC pattern

Within my application, I have two pages - page A and page B, each with its own model. Both pages are loaded by the HomeController as shown below. public class ModelA { public string UserName { get; set; } public int UserID { get; set; } } public ...

Tips for monitoring multiple values in a Vue 3 script setup

Within my code, I currently have a watcher set up like this (inside <script setup>): const form = reactive({ body: '', image: '' }) watch(() => form.image, () => { console.log(form.image) }) I am looking to enh ...

What is the equivalent of window.onload in emscripten?

In my HTML file, I have been copying and pasting the Emscripten generated code into the <script></script> region. However, the browsers tend to execute the Emscripten code before the entire HTML file has been processed. This causes issues if ...

What is the best way to bring a JavaScript file from the source file into an index.html document

I am currently in the process of developing a system using React, and as someone new to the framework, I have encountered an issue. I need to include a JavaScript file in my index.html that is located within the src folder. This js file is essential for th ...

Discover data using JavaScript recursively through paths

Here is the data structure I am working with: [ { name: 'root', children: [ { name: 'page', children: [ // and so on ] } ] } ] I am in need of a function that can retrieve ...

Reflect the values from the textarea onto the append

http://jsbin.com/renabuvu/1/edit In my current project, I am working on a feature where I can type a CSS selector, modify its values, and see the changes reflected in real-time on the page. This functionality is working smoothly without any issues. Howev ...

The function findOne() in MongoDB retrieves a value, however, in the node environment, it does not return the value

Having an issue with retrieving values from MongoDB in Node using a function. The data is being found within the findOne function, but it returns undefined when accessed outside of it. Check out the code snippet below: async function findFile(userid) { ...

Guide to customizing the sort arrow colors of b-table in Bootstrap 4 within Nuxt framework

https://i.sstatic.net/axVNu.png Using nuxt and bootstrap, I've noticed that the default sorting arrows in the table are too dark for my background. Is there a way to change the color of these sorting arrows? <b-table show-empty small ...

Obtaining the value of a checkbox with React

I have a form with two input fields (email, password) and a checkbox. The code for the form is shown below: <Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}> <TextField margin="normal" required ...

What is the process behind managing today's Google image of the day?

After coming across the javascript picture control on the Google search page, I became interested in replicating it myself. The feature zooms in on a picture when hovering over it, but I couldn't locate the HTML code they were using for it. Is there ...

Launch a modal by clicking a button located in a separate component using Vue JS

I am dealing with 2 components: TestSearchTool and TestModal. My goal is to display the Modal when a button in the TestSearchTool component is clicked. These two components are siblings in the hierarchy, so I am struggling to pass values between them (even ...

Running a script through the terminal using an electron js application, but the process necessitates the installation of node js

I am currently working on a project that involves running a script through the system terminal using my Electron.js application. However, I am facing an issue with new users who may not be technically proficient and do not have Node.js installed on their s ...

Learn the process of connecting checkboxes to a database in MeanStack using Angular

Hey everyone, I'm currently working with AngularJS ng-repeat and I am trying to dynamically bind a checkbox based on a true or false value from the database. However, even when the database value is set to true, the checkbox does not automatically che ...