How to combine date and time into a single object?
You just need to use the correct methods, instead of calling constructors. Use parse
to create local date and local time objects, then pass the two objects to the of
method of LocalDateTime
:
LocalDate datePart = LocalDate.parse("2013-01-02");
LocalTime timePart = LocalTime.parse("04:05:06");
LocalDateTime dt = LocalDateTime.of(datePart, timePart);
EDIT
Apparently, you need to combine two Date
objects instead of 2 strings. I guess you can first convert the two dates to strings using SimpleDateFormat
. Then use the methods shown above.
String startingDate = new SimpleDateFormat("yyyy-MM-dd").format(startDate);
String startingTime = new SimpleDateFormat("hh:mm:ss").format(startTime);
How to combine date and time into a single datetime object?
You can specify the format of your input string to let moment know how to parse it.
var date = '2019-02-16';var time = '8:24 PM';
// tell moment how to parse the input stringvar momentObj = moment(date + time, 'YYYY-MM-DDLT');
// conversionvar dateTime = momentObj.format('YYYY-MM-DDTHH:mm:s');
console.log(dateTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
R tick data : merging date and time into a single object
Create a datetime
object with as.POSIXct
:
as.POSIXct(paste(x$date, x$time), format="%Y-%m-%d %H:%M:%S")
[1] "2010-02-02 08:00:03 GMT" "2010-02-02 08:00:04 GMT" "2010-02-02 08:00:04 GMT"
[4] "2010-02-02 08:00:04 GMT" "2010-02-02 08:00:04 GMT"
How to combine date portion of one date and combine it with time portion of another date
Assuming your dates are in the ISO format and need another ISO date string as the result:
function start (date, time) {
return `${date.split("T")[0]}T${time.split("T")[1]}`;
}
console.log(start("2021-05-25T09:50:40.603Z", "2021-05-12T11:52:40.603Z"));
How to combine date and time into a Date object?
The Date()
constructor always works fine with string as Shyne also told it permits only valid ISO formatted string.
There are 4 ways of creating Date
objects with Date()
constructor, and one of those is:new Date(year, month, day, hours, minutes, seconds, milliseconds)
Considering your specific problem, a function can be written as:
function parseStringToDate(dateStr, timeStr) {
// create array of DD, MM, YYYY
let date = dateStr.split("-");
// convert timeStr to string if is in float value with fixed 2 decimal points
if (typeof (timeStr) !== 'string') {
timeStr = timeStr.toFixed(2).toString();
}
let time = timeStr.replace(".", ":"); // "." --> ":"
date.push(time); // ['07', '07', '2021', '8:30']
console.log(new Date(date).toString());
}
parseStringToDate('07-07-2021', '8.30');
parseStringToDate('07-07-2021', 8.00);
combine date and time into a single datetime
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;
}
Combine date and time string into single date with javascript
You can configure your date picker to return format like YYYY-mm-dd
(or any format that Date.parse
supports) and you could build a string in timepicker like:
var dateStringFromDP = '2013-05-16';
$('#timepicker').timepicker().on('changeTime.timepicker', function(e) {
var timeString = e.time.hour + ':' + e.time.minute + ':00';
var dateObj = new Date(datestringFromDP + ' ' + timeString);
});
javascript Date
object takes a string as the constructor param
Related Topics
In Java, How to Combine Two Json Arrays of Objects
Pass and Get Json String to Spring Controller
Java Input Validation for Number Range and Numeric Values Only With Counter
Simplifying the If-Else Condition Using Java Functional Style Programming
Java to Jackson Json Serialization: Money Fields
Name a File in Java to Include Date and Time Stamp
How to Use Limit in Spring Within SQL Query
Spring Data Repository @Query - Update and Return Modified Entity
Android Classnotfoundexception: Didn't Find Class on Path
Safely Generate Random Numbers Between Some Range in Java
Create List of Object from Another Using Java 8 Streams
How to Add Multiple Components to a Jframe
How to Find Max Date in List<Object>
Setting Default Values to Null Fields When Mapping With Jackson
How to Apply Spring Boot Filter Based on Url Pattern
Webdriver/Pageobject/Findby: How to Specify Xpath With Dynamic Value