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
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.
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.
We use the combine() function, and pass it the date object and the time object that we want to build our datetime out of.
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.
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">
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With