Obtain the UTC timestamp from the current local time on the same day

Imagine knowing the current time (hh:mm:ss) without the date, but you're certain it's today:

12:34:56

Additionally, you know that the time is in the CST timezone (UTC +0800). Your goal is to obtain a timestamp based on this time.

If we consider the present UTC time as 2014/08/25 17:10:00 and CST as 2014/08/26 01:10:00 (UTC +0800), indicating that CST is already on the next day. Therefore, using something like this won't work:

var d = new Date().toJSON().slice(0,10);
// "d" returns "2014-08-25" in local time -> this is incorrect

The objective is to convert:

1:10 (CST) --> to 2014/08/25 17:10 (UTC) or its corresponding timestamp 1408986600

Is it possible to determine the complete date-time or timestamp when having knowledge of only the time and timezone?

Answer №1

If you want to customize the attributes of a Date object, you have the option to manipulate them directly:

let currentTime = new Date();

currentTime.setHours(7);
currentTime.setMinutes(30);
currentTime.setSeconds(45);

console.log(currentTime.toTimeString());

Answer №2

This solution seems to provide an efficient method using moment-timezone.js:

// Representing time as hh:mm:ss in CST timezone
var cst_time = '1:10:00'; 

// Obtain today's date in the CST timezone
var cst_today = moment.tz("Asia/Shanghai").format("YYYY-MM-DD"); 

// Convert the given date and time from CST to UTC and format it accordingly
var timestamp = moment(cst_today + " " + cst_time + " +0800").utc().format("YYYY/MM/DD HH:mm:ss");

// The output should be "2014/08/25 17:10:00", which appears to be accurate
console.log(timestamp); 

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

Prevent infinite scrolling from continuing once the end of the array is reached in React JS

My React app fetches the Flickr public feed, allowing users to scroll through endless content. However, I'm facing an issue where the app keeps trying to load more content even when it reaches the end of the list. This is evident from the loading mess ...

Looking to extract, interpret, and display PDF documents using React?

I'm working on displaying a PDF file from an external URL. My goal is to extract text from the PDF for analysis, and then present a specific section of a page to users in my React application. This will involve cropping the content using coordinates o ...

PHP: Clarifying Column Names when Joining with Mysqli

In the scenario where data is lost due to conflicting column names in a JOIN operation, particularly when using PHP's mysqli and returning the result as a JSON object, there arises an issue. Let's consider a situation where two tables are relate ...

Implementing a hamburger menu and social media sharing buttons on websites

Currently working on my personal website and delving into the world of Web Development, I have some inquiries for seasoned developers. My first query revolves around incorporating a hamburger menu onto my site for easy navigation to other pages. After att ...

Picking out text areas enclosed by div elements

It seems like I find myself coming back here quite frequently to ask for help, but once again I am stuck. I am trying to select a textarea and enable editing in another textarea, which works fine with textboxes but not with textareas. Every time I click on ...

Error: Encountered an unexpected token while trying to parse multiple form functions

I recently added the following submission function to my .js file: $( "form" ).on( "submit", function( event ) { event.preventDefault(); var data = $( this ).serialize(); $.ajax({ type: "POST", url: "content/rev/a_sub ...

Using AJAX and JQuery to automatically refresh a webpage on a scheduled interval

I am attempting to automatically refresh a page every 5 seconds with updated data from an SQL Server. Below is my code snippet: @model Test.Data.Domain.ManufacturingCdMachine @{ ViewBag.Title = "Rimage Details"; ViewBag.JobId = Model.CurrentManufa ...

Is there a way to verify if a button has been clicked in node.js?

I need to determine if a button has been clicked and then display a new HTML file. I am currently using the express.js app.get method to open my HTML files. Can someone please explain how I can direct a button click to open a new HTML file in the same br ...

What is the best way to retrieve a value from $http or $resource using this filter?

Within my HTML, I have a structure that looks like {{ i.userId | userName }}. Essentially, I am attempting to convert this into a name by utilizing a filter. However, I am encountering numerous challenges with the asynchronous function required to retrieve ...

wordpress widget that triggers a function when a click event occurs on jquery

I'm facing an issue where this function works perfectly in my header, but seems to have no effect when used in a widget $("#myID").click(function(){ var name = $("#name").val(); var phone = $("#phone").val(); console.log(name +" "+ phone) ...

Retrieving outcome of Solidity contract function using web3-1.0.0-beta.27

I am using web3 1.0.0-beta.27 and the pragma solidity is set to ^0.4.2. contract Charity{ function ping() public constant returns (uint) { return 200; } } Currently, I am compiling and calling it in typescript with: import * as fs ...

Running the command "npm install [package]" does not automatically update the package.json file

I'm currently utilizing Laravel and experimenting with angular-ui-sortable and angular-utils-pagination. I have successfully installed them using npm, however, I am facing difficulties in updating the package.json file for angular-utils-pagination. ...

Make sure to always send back JSON data when using the default error handler in ExpressJS

I'm in the process of developing an API server using ExpressJS. I want to guarantee that the server consistently delivers JSON data rather than HTML data. While I can easily make the server respond with JSON for all customized routes, I encounter diff ...

Animating a vertical line moving gracefully between two points

I am facing a challenge that requires your expertise. You may have come across an animated line resembling a scanner in various applications. I need something similar, but in the form of a graph. Specifically, I am looking to create a vertical line that a ...

Tips for customizing the appearance of a timer using javascript and css

Check out this fiddle linked here: jsfiddle First, have a look at the initial HTML code below. For the JavaScript part, refer to the fiddle: <div id="test_time_left">Time Left :<b><span id="time_value" style="padding-left:5px;"></spa ...

What is the best way to create a visual separation between these two arrows using styled-components?

Currently, I am immersing myself in styled-components and attempting to replicate the image provided below. Although I can achieve this with CSS, I am solely focusing on utilizing styled components for this task. <Container> {sliderItems.map((item) ...

What is the method for generating ocean waves using three.js?

I attempted to create ocean waves using three.js, but when I tried running it on a web browser, all I saw was a blank white screen. https://i.sstatic.net/dhEsY.png The code snippet I utilized for generating the ocean waves is: <!DOCTYPE html> <h ...

A recursive function stuck in an endless loop, going on indefinitely

I have a recursive function that is supposed to call itself until the coordinates exceed the boundaries of a DOM table or until the distance between the starting point and the current recursion point is greater than the user-defined distance. However, the ...

Issue with base64 in Discord.js captcha channel system

Greetings everyone, and a special welcome to those willing to assist me. I am currently working on a project that involves verifying users through a hash decryption challenge system (base64). However, I've encountered an issue where the verification p ...

Retrieve JSON data generated within a JavaScript-powered webpage

My issue involves two HTML pages: 1) The first HTML Page (page1.html): <html lang="en"> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script> <script type="text/ ...