Changing Coordinated Universal Time to Pacific Standard Time with Moment Timezone in JavaScript

I am attempting to set the variable "formattedLocalTime" to Pacific time using the following snippet of code.

Despite the fact that the chrome debugger shows "locTime" as "Tue Sep 30 2014 16:17:25," which is the expected value, the formattedLocalTime on the last line displays "09/30/2014 11:17 pm," which is in UTC time and not the specified timezone of America/Los_Angeles. It should actually be "09/30/2014 4:17 pm" instead of 11:17.

Any suggestions would be greatly appreciated.

var timestamp = 1412144245453;                      // Tue Sep 30 2014 23:17:25
var utc = moment.tz(timestamp, "Etc/UTC");          // Tue Sep 30 2014 23:17:25 (displayed in chrome debugger)
var locTime = utc.clone().tz("America/Los_Angeles"); // Tue Sep 30 2014 16:17:25 (displayed in chrome debugger)
var formattedLocalTime = moment(locTime).format("MM/DD/YYYY h:mm a")

Answer №1

If you want to convert a timestamp to a specific time zone and format it in one step, you can use the following code:

moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a')

For the given timestamp, the output will be "09/30/2014 11:17 pm".

It's important to note that the timestamp corresponds to October 1st, 2014 6:17:25 AM in UTC time. However, when converted to Pacific time, it reflects September 30th, 2014, 11:17:25 PM.

You can verify this conversion by using a website like epochconverter.com or by utilizing moment.js as shown below:

moment.utc(1412144245453).format()   // "2014-10-01T06:17:25+00:00"

Answer №2

Consider utilizing the following:

let formattedLocalTime = locTime.format("MM/DD/YYYY h:mm a")

If you use moment(locTime), your date and time will revert back to local time

Answer №3

Use: moment-timezone - Error: moment().tz is throwing a TypeError

const moment = require('moment-timezone');
const currentTime = moment.tz(1567812345678, 'Asia/Tokyo').format('MM/DD/YYYY h:mm a');
console.log("Current time : ", currentTime);

Output: Current time : 09/30/2019 5:31 pm

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

How can I change an array to a string in JavaScript/jQuery and add a specific

Currently, I am tasked with the challenge of converting an array into objects. Furthermore, I also need to add something before each object is displayed. var arrayList = ["image1.jpg","image2.jpg","image3.jpg"]; The desired outcome should look like this ...

Steps for effectively deleting browsing history on Ionic

While testing my app on Chrome browser, I encountered an issue with clearing old views and cache. This is the process I followed to sign out of my app: https://i.stack.imgur.com/EGgRx.png Upon clicking Log out, the following code is executed: Auth.$s ...

What is the best way to establish communication between a server and client using sockets in Node.js?

Presently, I am in the process of developing a JavaScript application using AppJS. I'm encountering some difficulties understanding the communication between the client and server. Issue with Menu Bar and Socket Interaction The main challenge lies ...

The CORS middleware seems to be ineffective when used in the context of Node.js

I have set up my REST API on a Raspberry Pi server and connected it to the public using localtunnel. I am trying to access this API from a localhost Node.js application. The Node application is running on Express and includes some static JS files. However, ...

Activate a dropdown menu following the selection of a date on a calendar input using vue.js

What I need to do is have two select fields, one for 'days' and the other for 'hours'. When a day is selected, the user should then be able to choose between two available time slots. If they choose day two, the available time slots sh ...

Discovering the specific object ID that triggered an event in JavaScript

I am developing a webpage that includes JavaScript functionality. There is a specific function in the Javascript code which gets triggered by two different elements when clicked: 1. When a checkbox is clicked: $('#chkShowAll').click( functi ...

Django view receives all data in QueryDict format when using Angular to send data

As I work with Angular 1.4.4 and Django 1.8, I encountered an issue when posting data from Angular to Django view. The data is received in the form of a whole key in the QueryDict. Upon evaluating request.POST in debug mode, it returns: <QueryDict: {u ...

What are the limitations of using $aside in AngularJS?

I have a query regarding AngularJS. I attempted to open a dialog using $aside but encountered an injection error when trying this code after installing bower install angular-aside. The error message read Failed to instantiate module IndexHome due to:. If t ...

Just starting out with JS/jQuery and having trouble hiding a div as I thought it should (or revealing it incorrectly)

The issue can be observed by visiting . Upon clicking on a location name, a home "button" appears in the bottom left corner. Clicking this home button is supposed to revert back to the original page layout and hide the button. However, as soon as the curso ...

Validating Forms using Javascript

I'm struggling to understand why I'm not getting any error or success messages when I click the submit button. My goal is to validate strings in an HTML form to ensure they are not null or too long. Both the developer's tools and the Online ...

Update information interactively in Vuejs for all stored data

I am facing an issue with my code where the totalUserPosts value is returning as Zero after an AJAX call, and I need to find a solution for this problem. new Vue({ el: "#app", data: { totalUserPosts:0, }, methods:{ getPostsAjax () { $.ajax({ ...

Perform simple arithmetic operations between a number and a string using Angular within an HTML context

I'm stuck trying to find a straightforward solution to this problem. The array of objects I have contains two values: team.seed: number, team.placement: string In the team.placement, there are simple strings like 7 to indicate 7th place or something ...

Loading SVG images in advance

I am in possession of around one hundred simple SVG images, distributed among five different image folders. These images are currently retrieved on demand when they need to be displayed, which generally works well but occasionally results in flickering tha ...

Utilizing constants within if statements in JavaScript/TypeScript

When working with PHP, it is common practice to declare variables inside if statement parenthesis like so: if ($myvar = myfunction()) { // perform actions using $myvar } Is there an equivalent approach in JavaScript or TypeScript?: if (const myvar = myf ...

Tips for sending an API request from a local Ionic server to a separate local Express server

I currently have two applications running on my local machine: an ionic2 app running on http://localhost:8041 and an express app running on http://localhost:8000. Using Angular 2 observables, I am encountering an issue when making API calls with absolute p ...

JavaScript recursive reduce function

Looking to filter through an Array of objects and extract only those with the key is_enabled=true from another Array of objects. Structure of the data: [ { 'id': 1, 'label': 'Label1', 'option ...

` ` Despite entering correct values, the HTML form is still displaying validation errors due to JavaScript. ``

My contact form with validations is functioning well, but I am facing an issue where old errors persist even after correcting them and submitting the form again. Below is a snippet of the document containing the JavaScript code: <form id="contactForm" ...

Encase the entire section following cloning with JQuery

Make sure to check out the jsfiddle demo linked below before proceeding: JSFIDDLE I need to transform the original structure as shown below: <div class="out"> <a>im here</a> <a>to save</a> <a>our</a> ...

Troubles with Express JS POST Requests

I'm facing an issue while attempting to write code that creates a MongoDB entry using express.js. Every time I test my code with a cURL request, I receive an error message stating "empty response from server". Below is the snippet of my express.js co ...

JavaScript decimal precision function malfunctioning with currency format conversion

My current snippet works perfectly with the currency format 249.00, but I need to adapt it for a site that uses euros with pricing in the format 249,00. When I make this change, the total calculation goes haywire and shows as 29.900,00. Can someone advise ...