JQuery datetime down to milliseconds

I have a web application where I want to send information to a database.

I have a datepicker that allows the user to select a date and format the date as "YYYY-MM-DD"

. In addition to this, users must also select a time using timepicker

which formats the time as "HH:MM"

. He combined in a string DateTime

like "YYYY-MM-DD HH:MM"

.

I need to convert this to milliseconds so that the datetime is accepted as the correct format in the database (locale format YYYY-MM-DD HH:MM:SS.mmm

).

I tried many solutions found here and elsewhere to try and convert them to milliseconds. Whenever I try to concat and then convert, I usually get a NaN or "invalid date" error and I can't just add the converted milliseconds.

Is there a way to do this in jQuery or JavaScript?

+3


source to share


6 answers


I managed to figure it out. Thanks to those who answered. This is not a perfect solution, but it works.

var d = $("#date").val();
var dateParts = new Date((Number(d.split("-")[0])), (Number(d.split("-")[1]) - 1), (Number(d.split("-")[2])));
var dateis = dateParts.getTime();

var timeEnd = $("#endtime").val();
var time1 = ((Number(timeEnd.split(':')[0]) * 60 + Number(timeEnd.split(':')[1]) * 60) * 60) * 1000;

var timeStart = $("#starttime").val();
var time2 = ((Number(timeStart.split(':')[0]) * 60 + Number(timeStart.split(':')[1]) * 60) * 60) * 1000;

var dateTimeEnd = dateis + time1;
var dateTimeStart = dateis + time2;

      



What it basically does is take the date from the datepicker, and the start and end time from the timepicker. Ajax takes 2 days, one for the beginning, one for the end. The above solution basically takes all values ​​from input values ​​and converts them to milliseconds. It's not the best way to do things, but it's a quick fix.

+3


source


>> var datetime = new Date();
undefined
>> datetime.getTime();
1332613433314

      

Date.getTime () returns the number of milliseconds since 1970/01/01:



This needs to be handled server side.

+5


source


I don't understand your actual question, but I made a code that sets the datepixer to the minimum selected day as the code looks like this today:

$("#datefield").datepicker({
    dateFormat:"yy-mm-dd",
    minDate:new Date(new Date().getTime())
});

      

The return value new Date().getTime()

is milliseconds from 1970/01/01 05:30 am

(like my system)

+3


source


Can JavaScript Date Object Be Used?

You can use it like this: var d = new Date(yyyy, MM, dd, hh, mm, 0, 0).getTime();

You initialize a Date object and then use the getTime function to return the number of milliseconds since January 1, 1970.

+1


source


I would do it server side and use the strtotime function to convert to a timestamp which will give you a few seconds which if you really need milliseconds for some kind of script: 1 second = 1000 milliseconds.

If you can use jquery to check that you send valid time to the server side of the script before you do.

0


source


..

$ ("# DateField") DatePicker ("GetDate") GetTime (); will do the trick

0


source







All Articles