JavaScript Time conversion to ISO string is not correct with text field displayed time string conversion,python time string conversion,perl time conversion

I have a text field in an MVC application that displays the date and time. The date is created using a datepicker and the time I add to the text field. An example of the text is as follows:

10/23/2015 12:00 AM

I want to create a Date object with this time in ISO 8601 format. The date will be used to create an event in a fullcalendar jquery plugin. When I try to create the Date object, the ISO string is:

2015-10-23T04:00:00.000Z

It should be 2015-10-23T00:00:00.000Z to represent midnight on that day. This is my code:

<label id="schedule_start_date_lbl">Start Date: </label>
<input id="schedule_start_date" type="text" />

var startDayIndex = getDayIndex($('#rotation_start_time_txt').val());
$("#schedule_start_date").datepicker({
    minDate: 0,
    beforeShowDay: function (date) { return [date.getDay() == startDayIndex, ""] },
    onSelect: function (dateText) {
        $('#schedule_end_date').datepicker('option', 'minDate', dateText);
    },
    onClose: function (selectedDate) {           
        var rotation_txt = $('#rotation_start_time_txt').val();
        var time = rotation_txt.substr(rotation_txt.indexOf(',') + 1);
        $(this).val(selectedDate + time.toString(' HH:mm tt').toString());
    }
});

    $("#rotation_schedule_btn").click(function () {
//text value in schedule_start_date is: 10/23/2015 12:00 AM
 var startDate = new Date($('#schedule_start_date').val()).toISOString();
 //Value displayed is 2015-10-23T04:00:00.000Z
 alert('startDate: ' + startDate);
});

Why is the time value off by 4 hours? Thanks

UPDATE I need to add to the Date objects after they are created. Creating the Date object using the function in the answer below is not creating it in UTC format. It can be displayed in that format but when I am creating events in the fullcalendar control, the date must be in UTC format and they are not. This is my function to create a schedule:

 $("#rotation_schedule_btn").click(function () {
    //create member list order    
    var memberList = [];         
    $("#rotationList li").each(function () {
        memberList.push({
            id: $(this).attr('id'),
            name: $(this).text(),
            color: $(this).css('background-color')
        })
    });

    //start and end date and time for new schedule
    var startDate = convertTextToDate($('#schedule_start_date').val())
    var endDate = convertTextToDate($('#schedule_end_date').val());

    //remove events between startDate & endDate
    $('#edit_calendar').fullCalendar('removeEvents', function (event) {
        if (event.start.toDate() >= startDate && event.start.toDate() <= endDate
               || event.end.toDate() >= startDate && event.end.toDate() <= endDate) {
            return true;
        }
    });

    //Create events from rotation schedule selected
    var newEvents = [];
    var rotation_length = $('#rotation_type_select option:selected').val();
    var rotation_start_date = new Date(startDate.toISOString());
    var rotation_end_date = new Date(startDate.toISOString());

    //End date is to midnight
    endDate.setMinutes(endDate.getMinutes() + 1);
    rotation_end_date.setDate(rotation_end_date.getDate() + parseInt(rotation_length));

    var member_index = 0;
    while (rotation_end_date <= endDate)
    {
   //     alert('start date: ' + rotation_start_date.toISOString() + ' end date: ' + rotation_end_date.toISOString());
        var event = new Object();
        event = {
            title: memberList[member_index].name,
            start: new Date (rotation_start_date.toISOString()),
            end: new Date (rotation_end_date.toISOString()),
            objectID: memberList[member_index].id,
            color: memberList[member_index].color,
            allDay: true,
            textColor: 'white'
        };
        newEvents.push(event);
        eventsAdded.push(event);

        rotation_start_date.setDate(rotation_start_date.getDate() + parseInt(rotation_length));
        rotation_end_date.setDate(rotation_end_date.getDate() + parseInt(rotation_length));

        if ((memberList.length - 1) == member_index) {
            member_index = 0;
        }
        else {
            member_index++;
        }
    }           
    //Render events on calendar
     $('#edit_calendar').fullCalendar('addEventSource', newEvents);
    }); //end create schedule button click

The function to convert the dates is the same as below but I renamed the function:

function convertTextToDate(dateValue)
{   
    var dateArray = dateValue.split(/\D/);
    var pm = /pm$/i.test(dateValue);
    var hour = (+dateArray[3] || 0) % 12 + (pm ? 12 : 0);
    var date = new Date(Date.UTC(dateArray[2], dateArray[0] - 1, dateArray[1], hour, +dateArray[4] || 0, +dateArray[5] || 0));
    return date;
}

What am i doing wrong?

Answer:1

Date.prototype.toISOString() is based on a 0 offset UTC timezone. So it is trying to normalize your current timezone back to 0 UTC, because when you do new Date() you create a new date based on your current time zone, that is why the method is trying to revert your date back to UTC time zone. You are probably right now in a time zone which is 4h behind UTC.

http://devdocs.io/javascript/global_objects/date/toisostring

In order to fix this, just append your string with " UTC" and it will no longer do the timezone shifting:

10/23/2015 12:00 AM UTC

It will generate:

"2015-10-23T00:00:00.000Z"
Answer:2

I'm trying to style the child component of a styled-component, but it sends the css to the parent instead of the child/ This is my code, export const Card = styled.div` position: relative; ${...

I'm trying to style the child component of a styled-component, but it sends the css to the parent instead of the child/ This is my code, export const Card = styled.div` position: relative; ${...

  1. style child components angular 2
  2. style child components
  3. styled components parent child
  4. angular style child components
  5. vue style child components
  6. react style child components
  7. angular style child components from parent

html <div ng-repeat="item in timesheets">{{item.week}}</div> script .controller('TimesheetMainCtrl', function ($rootScope, $scope, $window) { dpd.timesheetold.get(function (...

html <div ng-repeat="item in timesheets">{{item.week}}</div> script .controller('TimesheetMainCtrl', function ($rootScope, $scope, $window) { dpd.timesheetold.get(function (...

in server.js var connect = require('connect'); var http = require('http'); function doFirst(request, response, next) { console.log('bacon'); next(); } function doSecond(request, response, ...

in server.js var connect = require('connect'); var http = require('http'); function doFirst(request, response, next) { console.log('bacon'); next(); } function doSecond(request, response, ...

  1. cannot read property of undefined
  2. cannot read property 'length' of undefined
  3. cannot read property 'setstate' of undefined
  4. cannot read property 'push' of undefined
  5. cannot read property 'map' of undefined
  6. cannot read property 'addeventlistener' of null
  7. cannot read property 'match' of undefined
  8. cannot read property 'then' of undefined
  9. cannot read property '0' of undefined
  10. cannot read property 'props' of undefined
  11. cannot read property 'nativeelement' of undefined
  12. cannot read property 'tolowercase' of undefined
  13. cannot read property 'state' of undefined
  14. cannot read property 'foreach' of undefined
  15. cannot read property 'split' of undefined
  16. cannot read property of null
  17. cannot read property 'replace' of undefined
  18. cannot read property 'summary' of undefined
  19. cannot read property 'createelement' of undefined
  20. cannot read property 'subscribe' of undefined

I've just started learning javascript. My problem is, that the website is slowing down after a few seconds. I'm using setinterval to "tick" the things on the screen and i feel like this might be the ...

I've just started learning javascript. My problem is, that the website is slowing down after a few seconds. I'm using setinterval to "tick" the things on the screen and i feel like this might be the ...

  1. setinterval slows down