What is the best way to configure the time to T00:00:00.000Z in JS?

In my usage of ui-calendar, I am attempting to pass events without a specified time. Currently, I am retrieving the date and time in the following manner.

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var WOD = new Date(y,m,d);

Moreover, I have a function that updates WOD to the selected day in the month view of the calendar like so.

$scope.alertOnDayClick = function(d){
    WOD = d;
};

When clicking on a specific day (e.g., Jan 22, 2015), the result is as desired: WOD = "2015-01-22T00:00:00.000Z", which works perfectly.

However, in the absence of selecting a day, it defaults to WOD = "2015-01-04T05:00:00.000Z".

My aim is to have the default date contain the time T00:00:00.000Z.

Various attempts have been made, including the following:

var WOD = new Date(y,m,d,0,0,0,0)
var WOD = date.UTF(y,m,d)
var WOD = date.UTF(y,m,d,0,0,0,0)

Additionally, setting the default time to null and exploring resources like Dealing with DateTime format for international application has been considered, although the implementation remains unclear.

Any assistance offered would be highly appreciated.

An update on the situation reveals that even after implementing suggestions from What is the best way to initialize a JavaScript Date to midnight?, the issue persists.

For instance, using the code snippet below:

var WOD = new Date();
    WOD.setHours(0,0,0,0);

Results in "2015-01-04T05:00:00.000Z", consequently affecting the stored event on Jan 4 at 12 am.

Further attempts and refinements have been made, as exemplified in the updated controller below. Despite efforts, the issue remains unresolved.

var myAppModule = angular.module('MyApp', ['ui.calendar']);
myAppModule.controller('MyController', function($scope,$compile,uiCalendarConfig) {
    // controller code here
});

Answer №1

Looks like you may have a time zone difference of +5 hours.

var WOD = new Date();
    WOD.setHours(0,0,0,0);// Sun Jan 04 2015 00:00:00 GMT-0600 (CST)

var WOD = new Date();
    WOD.setUTCHours(0,0,0,0);// Sat Jan 03 2015 18:00:00 GMT-0600 (CST)

The discrepancy could be due to the settings on YOUR DEVICE. Double check the "Time Zone" settings.

Answer №2

Take a look at the demo provided below;

var application = angular.module('application', []);

application.controller('primaryCtrl', function($scope) {

  $scope.currentDate = new Date();

  $scope.currentDate.setHours(0, 0, 0, 0);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="application">
  <div ng-controller="primaryCtrl">
    {{currentDate}}
  </div>
</body>

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

Having trouble with nested requests and appending using Jquery or JavaScript?

Greetings everyone, I want to apologize in advance for any spelling errors or mistakes in my message. I struggle with dyslexia and other learning difficulties, so please bear with me. I am using this time during lockdown to learn something new. This is my ...

Guide to downloading a CSV file directly from a webpage built with vue.js

Delving into the world of vue.js, I find myself pondering over how to incorporate a download link in my webpage for a CSV file stored locally. In my component Template.vue, I have the following structure: <a :href="item.loc" download> {{item.title ...

Codemirror - Advanced Auto-Suggest Feature with Separator

Can a separator be easily added in the hints/autocomplete addon? This separator would help transform the suggestion box to look like: f1 f2 f3 --- var1 var2 ...

Struggling to create a regular expression for a particular scenario

I'm dealing with nodes and currently faced with the task of applying a UNIX-like grep command to filter out specific content from an HTTP GET response. Below is the raw text received as the body variable: <?xml version="1.0" encoding="UTF-8" stand ...

Connecting to deeply nested attributes within an object using specified criteria

I apologize if the title of my query is not very descriptive, I couldn't come up with a better one. Please feel free to suggest improvements. I am currently working on developing a reusable "property grid" in Angular. My goal is to create a grid wher ...

Can someone please provide a reference to an online illustration of implementing TinyMCE with a print format similar to Microsoft Word

Are there any online resources demonstrating the use of TinyMCE with a print layout resembling Word, with visible page breaks? Alternatively, are there any other WYSIWYG editors with a built-in print layout feature available for open source use? ...

Transform JSON data into HTML format while excluding particular values

I recently integrated a JSON API that fetches event data. Here's a snippet of the JSON structure: { "id":1, "status":"ok", "start":{ "date":"2021-01-16" } } To display this ...

Tips for ensuring that your website can recall which call-to-action (CTA) has been selected for redirection

I'm attempting to create a bilingual webpage in Spanish and English. My goal is to have a main page where the user selects the language, and once chosen, the page remembers the language preference for future visits instead of prompting the choice agai ...

Creating a basic voting system with Vue.js and Firebase: A step-by-step guide

Hello, and please forgive me if this question seems too basic, but I'm just starting out and feeling a bit overwhelmed. I'm attempting to allow a user to vote on and then remove their vote from an item posted by another user using the same butto ...

Issue with Material UI button not properly redirecting to specified path

Having an issue with a button that should redirect to a specific component with props on click. <Button variant="contained" color="primary" className={classes.button} endIcon={<SendIcon/>} onClick={() => { <Redirect ...

Unexpected Behavior in ComponentDidMount

During my exploration of the React documentation, I came across an example of a clock timer implementation. It was interesting to see how the setInterval function is used inside the componentDidMount method to update the state and trigger re-rendering of ...

Tips for eliminating null values from a JavaScript object

I am currently facing an issue with a JavaScript object that consists of two arrays. At times, one of the arrays might be empty. I am attempting to iterate through the object using a recursive function, but I want to exclude any empty arrays or strings fro ...

Querying MySQL DATETIME values to return only the DATE in JPQL

query = em.createQuery("SELECT COUNT(a) FROM Appointment a WHERE a.datetime >= ?1 and a.datetime < ?2"); query.setParameter(1, date1, TemporalType.DATE); query.setParameter(1, date2, TemporalType.DATE); Unfortunately, JPQL does not support a DATE() ...

Encountering a problem with Node.js because of a SyntaxError: Receiving an

Recently, I decided to dive into learning node.js and attempted something very basic: displaying a "Hello World" message from the server. The code snippet I used (taken directly from a book) is as follows: var http = require("http"); http.createServer(fu ...

Angular File Sorting Error in Gulp detected

After including .pipe(angularFilesort()) in my gulp script, the task runs wiredep but never proceeds to the default task. It just stops after executing wiredep. However, if I remove .pipe(angularFilesort()), the script works perfectly fine. Can someone h ...

Changing the URI in accordance with the previous URI

I am encountering an issue in my React application where multiple updates of the URI are being made within the same event by adding query parameters using the router.push function from various locations in the code. However, some updates are getting lost b ...

What could be causing my select tags to appear incorrectly in Firefox and IE?

With the help of Jquery, my goal is to dynamically populate a select field when it is in focus, even if it already has a value. Once populated, I want to retain the previous value if it exists as an option in the newly populated field. Although this works ...

What could be the issue with trying to bind an event handler in this manner?

I'm having some trouble binding an event handler with jQuery: $(document).ready(function () { var newsScrollerForPage = new NewsScroller(); newsScrollerForPage.init(); $('#scroller-left-a').bind('on ...

What is the best way to utilize ngResource for a particular response body structure?

My reply body has the following structure: { "code": 200, "message": Data retrieved successfully, "items": [ { ... } ] } Previously, I used $http and did not encounter this issue. Howev ...

What is the significance of having 8 pending specs in E2E Protractor tests on Firefox?

Each time I execute my tests, the following results are displayed: There were 11 specs tested with 0 failures and there are 8 pending specs. The test execution took 56.861 seconds to complete. [launcher] There are no instances of WebDriver still running ...