Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate a date and time value

i need to concatenate a date value and a time value to make one value representing a datetime in javascript.

thanks, daniel

like image 455
danielea Avatar asked Nov 08 '09 20:11

danielea


2 Answers

Working with strings is fun and all, but let's suppose you have two datetimes and don't like relying on strings.

function combineDateWithTime(d, t)
{
   return new Date(
    d.getFullYear(),
    d.getMonth(),
    d.getDate(),
    t.getHours(),
    t.getMinutes(),
    t.getSeconds(),
    t.getMilliseconds()
    );
}

Test:

var taxDay = new Date(2016, 3, 15); // months are 0-indexed but years and dates aren't.
var clockout = new Date(0001, 0, 1, 17);
var timeToDoTaxes = combineDateWithTime(taxDay, clockout);
// yields: Fri Apr 15 2016 17:00:00 GMT-0700 (Pacific Daylight Time)
like image 79
user420667 Avatar answered Sep 29 '22 02:09

user420667


I could not make the accepted answer work so used moment.js

date = moment(selected_date + ' ' + selected_time, "YYYY-MM-DD HH:mm");

  date._i   "11-06-2014 13:30"
like image 29
Caner Avatar answered Sep 29 '22 02:09

Caner