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?
source to share
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.
source to share
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)
source to share
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.
source to share