0

I want to change date from time and to time, To milliseconds.

I written this code but while getting from json it's giving wrong date and time.

I want to change date from time and to time, To milliseconds. | I written this code but while getting from json it's giving wrong date and time. | I want to change date from time and to time, To milliseconds. | I written this code but while getting from json it's giving wrong date and time. | I want to change date from time and to time, To milliseconds. | I written this code but while getting from json it's giving wrong date and time. | I want to change date from time and to time, To milliseconds. |

I written this code but while getting from json it's giving wrong date and time. |

    var date = $('#datepicker').val();/* Date */
    var date_array = date.split('/')
    var formatted_date = date_array[1] + '/' + date_array[0] + '/' + date_array[2];
    var date_format = new Date(formatted_date);
    var gt_date = date_format.getTime();

    var from_time = $("#timepicker1").val();/* From Time */
    var from_time_array = from_time.split(':')
    var from_formatted_time = from_time_array[0] + ':' + from_time_array[1];
    var from_split_time_array = from_time_array[1].split(' ')
    var formatted_from_time = from_split_time_array[0] + ' ' + from_split_time_array[1];
    alert(from_time_array[0] + '-' + from_split_time_array[0] + '-' + from_split_time_array[1]);
    if(from_split_time_array[1] == 'pm')
    {
        var from_time_format = (((Number(from_time_array[0])+12) * 60 + Number(from_split_time_array[0]) * 60) * 60) * 1000;
    }
    else
    {
        var from_time_format = ((Number(from_time_array[0]) * 60 + Number(from_split_time_array[0]) * 60) * 60) * 1000;
    }

    var to_time = $("#timepicker2").val(); /* To Time */
    var to_time_array = to_time.split(':')
    var to_formatted_time = to_time_array[0] + ':' + to_time_array[1];
    var to_split_time_array = to_time_array[1].split(' ')
    var formatted_to_time = to_split_time_array[0] + ' ' + to_split_time_array[1];
    alert(to_time_array[0] + '-' + to_split_time_array[0] + '-' + to_split_time_array[1]);
    if(to_split_time_array[1] == 'pm')
    {
        var to_time_format = (((Number(to_time_array[0])+12) * 60 + Number(to_split_time_array[0]) * 60) * 60) * 1000;
    }
    else
    {
        var to_time_format = ((Number(to_time_array[0]) * 60 + Number(to_split_time_array[0]) * 60) * 60) * 1000;
    }


    var from_time_sec = gt_date + from_time_format;
    var to_time_sec = gt_date + to_time_format; /* ----------- */
Arun
  • 142
  • 1
  • 1
  • 9

1 Answers1

0

Something is seriously wrong with the formatting in your post :) But i would suggest that if you want to get milliseconds from a date object you should call getTime() on a date.

If you call getTime() on a plain new Date() object you'll get the current time in milliseconds:

var millis = new Date().getTime()

You probably don't want 'now' so you should call setters on your date to set the desired time:

var d = new Date();
d.setHour(14);
// and so on for minutes, seconds, date, month, year 
var millis = d.getTime();
  • setDate() - Sets the day of the month of a date object
  • setFullYear() - Sets the year of a date object
  • setHours() - Sets the hour of a date object
  • setMilliseconds() - Sets the milliseconds of a date object
  • setMinutes() - Set the minutes of a date object
  • setMonth() - Sets the month of a date object
  • setSeconds() - Set the seconds of a date object

You can check out other setters here.

Notes: i suggested a basic Javascript way. You should be always mindful of the timezone and test what is happening in every step so you don't make false assumptions. I would also suggest that you understand the difference between UTC and local timezones (because milliseconds are measured against the Unix Epoch which is January 1st, 1970, 00:00:00 UTC)

Sandman
  • 2,577
  • 2
  • 21
  • 32