Changing a datetime string into a specific unit of time (such as seconds) using Javascript

Struggling with converting a Rails timestamp into a usable format for JavaScript.

When alerting the time of an event, the string produced is: 2016-02-18T23:07:00.000-08:00. It doesn't appear to be a UTC string, according to this resource, and I'm unsure of the time format.

Attempted to convert using this method:

  var t = "2016-02-18T23:07:00.000-08:00"
  var u = t.to_time 

However, this returns "undefined." Seeking to convert this string in JavaScript to seconds or milliseconds. Is it achievable?

Answer №1

let date = "2016-02-18T23:07:00.000-08:00"
let parsedDate = Date.parse(date);

Answer №2

If you want to easily manipulate dates and times in JavaScript, consider using Moment.js:

moment('2016-02-18T23:07:00.000-08:00').format('LLL');

As a result, you'll get:

February 19, 2016 8:07 AM

Alternatively, you can parse a timestamp like this:

Date.parse('2016-02-18T23:07:00.000-08:00');

This method works perfectly fine.

Check out Moment.js here

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

Display the Slug in the Website URL upon clicking on a Blog Post using Angular and Strapi framework

When a user clicks on a blog, I want the URL to display the slug instead of the ID. My frontend framework is Angular and I have created a service to fetch data from the backend built with Strapi. Currently, when selecting a blog from the view, the URL disp ...

Changed over to a promise-oriented database, causing my login feature to malfunction completely

Although I can successfully register, when I am redirected to my game route, all I see is a blank Error page with [object Object] on the screen. This message also appears in my console periodically. Initially, I suspected an issue related to socket.io, bu ...

Issue with incremental static generation in version 13 not functioning as expected

Is ISR working for anyone in the NextJS 13 Beta version? I have set revalidate to 15 as follows. export const revalidate = 15; However, even after running `npm run build`, the page still remains a statically generated site (SSG). The symbol is showing u ...

The Google Books API initially displays only 10 results. To ensure that all results are shown, we can implement iteration to increment the startIndex until all results have

function bookSearch() { var search = document.getElementById('search').value document.getElementById('results').innerHTML = "" console.log(search) var startIndex = I have a requirement to continuously make Ajax calls ...

The 'innerText' property is not found in the 'Element' type

Currently, I am working with Typescript and Puppeteer. My goal is to extract the innerText from an element. const data = await page.$eval(selector, node => node.innerText); However, I encountered an error: The property 'innerText' is not ...

Trouble with locating newly created folder in package.json script on Windows 10

I am facing an issue in my Angular application where I am trying to generate a dist folder with scripts inside it, while keeping index.html in the root folder. I have tried using some flag options to achieve this but seem to be stuck. I attempted to automa ...

Toggle visibility between 2 distinct Angular components

In my application, I have a Parent component that contains two different child components: inquiryForm and inquiryResponse. In certain situations, I need to toggle the visibility of these components based on specific conditions: If a user clicks the subm ...

Best method for connecting user input with a class

I'm currently working with a WYSIWYG editor (Tinymce) that allows users to post YouTube videos. In order to make the video content responsive, I need to add the class "video-container" around any "iframe" tags inserted when users paste a YouTube link ...

Transitioning the image from one point to another

I am currently working on a unique single-page website and I have been experimenting with creating dynamic background animations that change as the user scrolls. Imagine my website is divided into four different sections, each with its own distinct backgro ...

Execute the following onclick event when the button is enabled

I am trying to display a popup when the "add to cart" button is clicked (but only when the button is active!). I thought about using a div that wraps the button and changes the class from "disabled" to "enabled". I have tried using jQuery, but it could als ...

The angular controller erroneously indicates that the modal form is valid even though it is not

I am currently working with a form that appears in a modal directive using bootstrap UI. The form consists of 2 input fields with html5 validation set to "required." Initially, in the controller, I tried to check if the form is valid before proceeding to ...

Utilizing Ajax for submitting data in a Spring application

I'm attempting to send a PUT request to the controller using AJAX. Here is my code: $().ready(function(){ $('#submit').click(function(){ var toUrl = '/users/' + $('#id').val() + '/profile'; ...

Stable and persistent popup window that remains at the forefront in a Chrome extension

Currently developing a Google Chrome extension and seeking assistance in creating a popup window that remains fixed in one corner while staying on top of all other windows. Here is a reference image for clarification: https://i.stack.imgur.com/IPw7N.jpg ...

Error message: "Angular 2 queryParams is causing a 'does not exist on type' issue"

My Angular2 service is designed to extract parameters from a URL, such as http://localhost:3001/?foobar=1236. import { Injectable } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import 'rxjs/add/opera ...

Is it possible for a submission of a form to modify the content length header, resulting in the request failing?

Issue Description: After binding a submit event to an AJAX post request in order to send a predetermined key-value pair to a PHP script, the expected message indicating successful communication is not received. Despite the fact that the submit event trig ...

Pandas now offers the capability to employ datetime.time objects as a specific type

As I analyze an Excel file, I come across the following data structure: A B 2015-09-05 15:05:32 2015-09-05 19:05:02 To read this file, I use df = pd.ExcelFile(filename).parse(..) The DataFrame's dtype shows that date ...

Managing Jawbone API OAuth access tokens using node.js (express and passport)

Is there anyone who has successfully completed the Jawbone's OAuth2.0 authentication process for their REST API? I am facing difficulty in understanding how to access and send the authorization_code in order to obtain the access_token as mentioned in ...

Simple Bootstrap Input Slider Configuration

I am attempting to create a simple setup for a bootstrap-style input slider, but I am facing some difficulties getting it to function properly. Desired Outcome: https://i.sstatic.net/Btfo3.png Actual Outcome: https://i.sstatic.net/0VnNv.png Resource / ...

Preserve multiple selected values post form submission using PHP, HTML, and JavaScript

How can I retain the selected values in a form after it is submitted? My current code functions correctly when only one value is selected, but does not maintain all selected values when multiple are chosen simultaneously. Any assistance would be apprecia ...

Compare the current return value to the previous value in the success callback of an Ajax request

I am currently running an Ajax request to a PHP DB script every 3 seconds, and I need to make a decision based on the result returned. The result is a timestamp. Let's say the ajax request is fired two times. I want to compare the first result with th ...