Tips for extracting only a portion of the JavaScript timestamp

I have a JavaScript timestamp that reads

Tue Sep 30 2014 12:02:50 GMT-0400 (EDT)
.

When I use the .getTime() method, I get 1412092970.768

Typically, this timestamp represents a specific time of today. My question is whether it's possible to extract only the day, month, and year while keeping the hour, minute, and second values at 0.

For our current scenario, the desired format would be

Tue Sep 30 2014 00:00:00 GMT-0400 (EDT)
.

I'm unsure about how to go about this conversion process. Converting to Unix timestamp using getTime() doesn't seem to provide clear instructions, and I am unable to set the time directly like in PHP.

Is there a solution to rectify this issue?

Thank you

Answer β„–1

To manipulate a date object, you can initialize it and then reset any unwanted components to zero or specify the desired components, for example:

date = new Date();
date.setHours(0);
date.setMinutes(0);

Alternatively, you can do something like this:

date = new Date(); // current date and time

newDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0 , 0, 0, 0);
// create a new date with only year/month/day values and time set to zero.

For more information on the constructor's arguments, refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Answer β„–2

Another approach would be to pass the epoch value to PHP:

JS:

let currentEpoch = Math.floor(Date.now() / 1000);

PHP:

$epochTimestamp = strtotime("@$currentEpoch");
echo date('Y', $epochTimestamp); //year

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

Unable to access placeholder information from the controller

I am new to implementing the mean stack. I attempted to view data from the controller, but encountered an error message in the web browser's console. Error: [$controller:ctrlreg] http://errors.angularjs.org/1.6.3/$controller/ctrlreg?p0=AppCtrl Stack ...

A regular expression in Javascript that can be used to identify at least one word starting with a number among multiple words, with a

Currently, I am utilizing JavaScript Regex to validate an input field based on specific conditions: The value must consist of only alphabets and numbers There should be a minimum of two words (more than two are allowed) Only one word can start with a num ...

Angular displaying a blank screen, even though the complete dataset is available

I am currently working on my first website using Angular and I've encountered a problem. When I click on 'view project', it should return the data specific to that item. The strange thing is, when I log my JavaScript console, I can see all t ...

How to toggle between arrays using ng-repeat

Currently, I am managing 3 arrays and wish to toggle between them using ng-repeat: $scope.fooDataObj = { array1:[{name:'john', id:'1'},{name:'jerry', id:'2'}], array2[{name:'bill', id:'1'},{name: ...

The issue arises with getInitialProps as it fails to pass data to the page component while attempting to retrieve initial information and subsequently modify it using a button

I am currently working on a component located at app\page.tsx in Next.js v13.4.12, and it includes a button. My goal is to have the button trigger the handleClick function when clicked. The issue I'm facing is that the getInitialProps function ...

Transform the array into an associative array

Below is the array I am working with: print_r($data); When printed in PHP, the result looks like this: $data=Array( [0] => stdClass Object ( [name] => location [value] =>lko ) [1] => stdClass Object ( [n ...

Jumping Iframe Anchor Link in Src

I'm facing a challenge with an iframe positioned in the center of a webpage. I want to show content from another page within the iframe, but not starting at the very top. To achieve this, I inserted an anchor into the src of my iframe, linked to an a ...

Updating an existing value with a cascading dropdown list

My JavaScript code dynamically populates a dropdown list called District based on the selection made by the user in another dropdown list called Department. Here is the snippet of the code: Firstly, I populate the Department dropdownlist and add a ' ...

Scaling Images using HTML and CSS

Hey there, I'm currently learning web development and running into a bit of trouble with creating responsive images. Can anyone give me some guidance on what changes I should make in the code below? Here's the HTML code: <div class="caro ...

Extracting Unprocessed Data with Node.js Express

I am currently working with an Express server that handles a login form page: const app = express(); // part A app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded()); app.get('/login', ...

Is it considered a best practice to utilize JavaScript for positioning elements on a

I recently started learning JavaScript and jQuery, and I've been using them to position elements on my website based on screen and window size. It's been really helpful, but I'm starting to wonder if it's a good practice since it makes ...

Utilize Discord.js to send a message and patiently await your response

I am facing an issue while trying to code a Discord bot. I'm struggling to make it wait until the user inputs 'Y' or 'N'. Specifically, I am currently working on the ban command and everything seems to be functioning well until the ...

Angular 2's Multi-select dropdown feature allows users to select multiple options

Recently, I encountered an issue with customizing CSS for angular2-multiselect-dropdown. I found the solution in this link: https://www.npmjs.com/package/angular2-multiselect-dropdown. I have included my code below. Any assistance in resolving this matter ...

Incorporate a 404 Not Found redirect into the getServerSideProps method in Next.js

I'm seeking guidance on best practices for handling an HTTP 404 error in a server-side rendered page when the requested page lacks a corresponding server-side resource. For instance, let's say the requested page is http://localhost:3000/places/5 ...

Change the behavior of an onclick event on the second click

I'm trying to make a button on my website that not only plays music when clicked but also changes the text inside the button to "Go to SoundCloud" and redirects to SoundCloud. Currently, I have been able to achieve both functions separately - redirect ...

Looking for a solution to troubleshoot issues with the updateServing function in JavaScript?

I am attempting to create a function that will calculate the portion sizes for the ingredients on my website. This function is located in the Recipe.js file and appears as follows: updateServings(type) { // Servings const newServings ...

Tips for triggering functions when a user closes the browser or tab in Angular 9

I've exhausted all my research efforts in trying to find a solution that actually works. The problem I am facing is getting two methods from two different services to run when the browser or tab is closed. I attempted using the fetch API, which worke ...

Using jQuery JSONP only functions with URLs from the same site

I am looking to send a request to an API that returns data in json format. The API is located on a sub-domain of the domain where my script will be running (although currently it's on a completely different domain for development, localhost). My unde ...

Can we improve the coding of this as it seems inefficient and uses up too much room?

Do you think there is a more efficient way to write this code? It seems quite impractical and takes up a lot of space. Essentially, it's about the random chance of obtaining a rarity, like acquiring an Uncommon sword. if (Math.random() * 100 < 100 ...