Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery datetime to milliseconds

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

I have a datepicker, which lets the user select a date and formats the date as "YYYY-MM-DD". In addition to this, the users must also select a time using a timepicker which formats the time as "HH:MM". This gets concatenated into a DateTime string as "YYYY-MM-DD HH:MM".

I need to convert this into milliseconds for the datetime to be accepted as the correct format on the database (locale format of YYYY-MM-DD HH:MM:SS.mmm).

I have a tried a host of solutions found here and elsewhere to try and convert into milliseconds. Whenever I try to concat then convert I usually get a NaN error or "invalid Date" and I cannot simply add the converted milliseconds.

Is there any way of doing this in jQuery or JavaScript?

like image 230
moikey Avatar asked Mar 24 '12 18:03

moikey


People also ask

How do you go from date to milliseconds?

Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.

How to get current time milliseconds in JavaScript?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

How do you find milliseconds?

To get the current time in milliseconds, you just need to convert the output of Sys. time to numeric, and multiply by 1000.

How to get time stamp from date?

Getting the Current Time Stamp If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.


2 Answers

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

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

This should be handled server-side, though.

like image 147
Kenny Meyer Avatar answered Oct 03 '22 19:10

Kenny Meyer


I managed to figure this one out myself. Thanks to those who answered. Its not an ideal 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 this basically does, is take a date from a datepicker, and a start and an endtime from a timepicker. The ajax accepts 2 datetimes, one for start, one for end. The above solution basically gets all the values from the input values, and converts it to milliseconds. It's not the best way of doing things but it is a quick fix.

like image 39
moikey Avatar answered Oct 03 '22 18:10

moikey