Obtaining the Time Zone from an Area Code Using JavaScript

Can a Script in JavaScript Determine the Time Zone Based on Area Code?

I possess the Area Codes of the User.

Answer №1

I extracted the details from this webpage and created a reference map:

const timeZoneMap = getTimeZoneMap()
// Check: https://stackoverflow.com/a/33561517/1762224
const phoneNumberPattern = /^[+]*[(]{0,1}\d{1,3}[)]{0,1}[-\s\./0-9]*$/g

console.log(timeZoneMap[extractAreaCode('(619) 555-5555')]) // California (PST)

function extractAreaCode(phoneNumber) {
  if (phoneNumberPattern.test(phoneNumber)) {
    return /\d{3}/.exec(phoneNumber)[0] // first three sequential
  }
  return null
}

function getTimeZoneMap() {
  return {
    "201" : { "timezone" : "EST", "state" : "New Jersey" },
    "202" : { "timezone" : "EST", "state" : "District of Columbia" },
    "203" : { "timezone" : "EST", "state" : "Connecticut" },
    "205" : { "timezone" : "CST", "state" : "Alabama" },

    ... (Remaining content removed for brevity)

    "980" : { "timezone" : "EST", "state" : "North Carolina" },
    "984" : { "timezone" : "EST", "state" : "North Carolina" },
    "985" : { "timezone" : "CST", "state" : "Louisiana" },
    "989" : { "timezone" : "EST", "state" : "Michigan" }
  }
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

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

String representation of a network error in Javascript

Is there a way to retrieve the actual error text in JavaScript when an error occurs on XMLHttpRequest? Even though it gets displayed in the console, it's challenging to access as a string. For example: var req = new XMLHttpRequest(); req.open(' ...

Issues with Autofocus in AngularJs on Firefox

Check out this straightforward directive to set autofocus: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_scope, _element) { $timeout(function(){ ...

Unable to utilize JavaScript objects extracted from JSON

Working on developing a web application using AngularJS has led me to encounter an issue. I have a PHP server that retrieves data from an SQL database and encodes it into JSON. Utilizing the Angular $http service on the client side, I am able to successful ...

Implementation issue with Hashids library in Vue.js causing functionality hiccups

I'm having trouble getting the library hashids to cooperate with vue.js The method I would like to use is: <template> <div class="container"> {{ hashids.encode('1') }} </div> </template> <script& ...

Using PHP to capture and display form data without refreshing the page

Hello, I am looking to retrieve the value of an input field and fetch it using AJAX without involving a database. How can I achieve this with AJAX? <form method="POST> <input type="text" name="input" id="card-code" value='<?php echo $c ...

Refresh ng-repeat following data entry or push in Angular Material dialog

Currently, I am facing an issue with adding a new object to an ng-repeat array. The array is populated with data fetched through an $http request. My goal is to input data in a dialog and pass it to a function that will then add the data as an object to th ...

jquery sliding down effect - glide gracefully with the slide down

I am interested in creating an effect on my page that is similar to HostGator's live chat feature. I have searched for solutions here and found something close to what I want to achieve. However, the issue is that the div starts at a height of 1px and ...

"Attempting to use a Chrome Extension to apply inline styles is not producing

Despite my attempts to search on Google, I have been unable to find a solution. I am currently working on developing a Chrome extension with the specific goal of changing/inserting the style.display property for one or more elements. This task would norma ...

What could be causing my iframe to not adjust its size according to its content?

My attempt at resizing a cross-site iframe is not working as expected, despite following the guidance provided here. The iframe on my page seems to remain unaltered in size, and I can't figure out what mistake I might be making. The current location ...

Translating Encryption from Javascript to Ruby

I have an application which utilizes HTML5 caching to enable offline functionality. When the app is offline, information is stored using JavaScript in localStorage and then transmitted to the server once online connectivity is restored. I am interested in ...

Using Angular to make GET requests with JSON data in PHP

Seeking assistance with connecting Angular frontend to PHP backend. Upon calling the service, I am receiving an empty array in the console. Controller: angular.module('pageModule') .controller('pageController', ['$scope', &a ...

Transitioning from the older version of meteor 1.8.3 to the latest version 2.6

I'm working with an older project that is currently using version 1.8.3 of a software that supports MongoDB 5 in Meteor 2.6. I have reviewed the changelog and migration guide and now I have a question: Should I upgrade directly from 1.8.3 to 2.6, or i ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...

Installing Vue JavaScript using the npm command

Embarking on my journey as a Vue JS learner, I took the plunge to install Vue and delve into creating web applications using it. So, I globally added npm Vue. npm install --global vue-cli This action resulted in: npm WARN deprecated [email protected]: T ...

Access the original source code using jQuery

Is there a way to retrieve the raw HTML code (as seen in Chrome's source code window) using JQuery without having quotes converted to &quot or HTML symbols converted to text? I have tried using html() and text(), but neither method seemed to give ...

Updating the value of an Angular select on change is not being reflected

If the select element is changed, the value should be set to something different from what was selected. There's a feature in place that only allows 4 item types to be selected at a time; if more than 4 are chosen, the value reverts back to its origin ...

Animating elements within Firefox can sometimes cause adjacent elements to shift positions

Currently, I am working on a single-page website that utilizes keyboard-controlled scrolling. To achieve the desired scroll effect, I have developed a JavaScript function that animates divs. Here is the JavaScript code: var current_page = "#first"; func ...

How much time can the browser dedicate to running JavaScript before moving on to loading a new page?

The webpage contains multiple hyperlinks. I want to monitor user clicks on these links. Whenever a user clicks on a link, an Ajax request is sent to my server for processing. The server then returns relevant data, which is further processed on the client ...

Verify the dialog box return with a text input box

I am trying to implement a confirmation dialog box for when a user wants to delete their account. This dialog box requires the user to enter their password in a textbox. However, I am struggling with catching the postback event and executing the relevant m ...

I'm noticing that my CSS is behaving differently than expected. Despite setting position: absolute, the output is displaying as inline-block instead of block. Why is this happening

div { width:200px; height:200px; position: absolute; } .first-container { background-color: #9AD0EC; } .second-container { background-color: red; left: 200px; } .third-container { background-color: blue; left:400px; } Despite setting th ...