0
<template>
  <VueDatePicker
    v-model="deliveryTime"
    time-picker
  />
</template>

<script setup>
import { ref } from 'vue'

const deliveryTime = ref(new Date())

// later sent like:
const payload = {
  DeliveryTime: deliveryTime.value
}
</script>

The user selects only time (e.g., 15:50). What is the correct way to convert that into a full JavaScript DateTime object (with today’s date and selected time) to send to an ASP.NET Core backend and save in a SQL Server database?

1 Answer 1

0

Given that VueDatePicker v-model stores a selected time value like this:

{ "hours": 13, "minutes": 36, "seconds": 0 }

You can use the following Date constructor

new Date(year, monthIndex, day, hours, minutes, seconds)

To create a computed value with the desired format

const payloadTime = computed(() => {
  const now = new Date();
  const time = deliveryTime.value;

  const dateTime = new Date(
    now.getFullYear(),
    now.getMonth(),
    now.getDate(),
    time.hours,
    time.minutes,
    time.seconds
  );
  // if dateTime is invalid, return new Date()
  if (isNaN(dateTime.valueOf())) return now;
  // else ok to return dateTime
  return dateTime;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.