Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine date and time into a single datetime [duplicate]

I have a date and time control, which I would like to concatenate

var startDate = jQuery('#startDatepicker').find("input").val();
var startTime = jQuery('#startTimepicker').find("input").val(); 

I have another var field which has data as below:

var targetTime = new Date().setMinutes(-5).valueOf(); 

startDatepicker has the value as : 02/06/2017

startTimepicker, currentTime has the value as : 05:17 am

targetTime has the value as: 1486374940591

I want to concatenate startdate and starttime in the format of targetTime. How to concatenate the start date and start time? Thanks

like image 787
venkat14 Avatar asked Feb 08 '17 05:02

venkat14


People also ask

How do I combine date and time in one column in Excel?

There is a very simple formula that can quickly help you combine date column and time column into one. Tip: You also can use this formula =A2+B2 and then format the result cells as date and time formatting.

How do I concatenate date and time to text in Excel?

To combine the date and time columns into one column, the following formula can help you: 1. Enter this formula: =concatenate(text(A2,"mm/dd/yyyy")&" "&text(B2,"hh:mm:ss")) into a blank cell where you want to put the combined result, then press Enter key to get the first combined cell.

Which command is used to combine both date and time?

We use the combine() function, and pass it the date object and the time object that we want to build our datetime out of.

How do you combine date and time in SAS?

Re: Combining Date and Time into one variable You can do this via a FORMAT statement. See the example below. data demo; date = today(); time = time(); date_time = dhms(date, 0, 0, time); format date_time datetime20.


2 Answers

This do what you need

var date = new Date(startDate + ' ' + startTime); 

jQuery('#startDatepicker, #startTimepicker').on('input', function() {
  var startDate = jQuery('#startDatepicker').val();
  var startTime = jQuery('#startTimepicker').val();
  var date = new Date(startDate + ' ' + startTime);
  console.log(date)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Date
<input id="startDatepicker">Time
<input id="startTimepicker">
like image 137
Parvez Rahaman Avatar answered Oct 01 '22 04:10

Parvez Rahaman


Pass your date and time to this function. It will return the Date object. Use getTime() on that to get the desired result. Codepen example.

function getAsDate(day, time)
{
 var hours = Number(time.match(/^(\d+)/)[1]);
 var minutes = Number(time.match(/:(\d+)/)[1]);
 var AMPM = time.match(/\s(.*)$/)[1];
 if(AMPM == "pm" && hours<12) hours = hours+12;
 if(AMPM == "am" && hours==12) hours = hours-12;
 var sHours = hours.toString();
 var sMinutes = minutes.toString();
 if(hours<10) sHours = "0" + sHours;
 if(minutes<10) sMinutes = "0" + sMinutes;
 time = sHours + ":" + sMinutes + ":00";
 var d = new Date(day);
 var n = d.toISOString().substring(0,10);
 var newDate = new Date(n+"T"+time);
 return newDate;
}
like image 23
Shah Abaz Khan Avatar answered Oct 01 '22 05:10

Shah Abaz Khan