Convert date to datetime (and give it default time) in Javascript - javascript

I have an input in my React app that prompts user to select a date without time.
I am creating a scheduler that sends notification at specific time, say 7:00 PM, the night before. So if user selects 5/17/2017, I want it to send reminder at 7:00 PM 5/16/2017. I can do the math subtracting the day and hours, if they are both in datetime format. However, one data is in date format and another is in datetime.
The input I am using is simple input with type="date". The output looks like 2017-05-25. I tried adding .setHours(15) but it didn't work.
Is there a way to convert any given date to datetime and give them default time in Javascript? If I choose 2017-5-17 it will be converted to Wed May 17 2017 19:00:00 GMT-0700 (PDT)

Related

Convert Time to Another Timezone with Considering Daylight Time Using JavaScript

I'm trying to convert specific time to my local timezone that is Asia/Tehran with this code:
var date = new Date("9/9/2021 17:30 UTC +1").toLocaleString('en-US', {
"timeZone": "Asia/Tehran"
})
Based on this link https://www.timeanddate.com/worldclock/converter.html?iso=20211009T163000&p1=3903&p2=246 I should get 20:00 time. But I'm getting 21:00.
I know this is because of daylight times in Iran, But how can I consider daylight times when I want to use the JavaScript toLocaleString function?
In the link, you have the month October, whereas in your code, you have September 🙂.

How can I convert GTM-0006 to ISO without Adding hours

I'm working in a from who has a date field and by default it shows the current date.
I set the date using this:
var date = new Date(); = Tue May 25 2021 17:06:01 GMT-0600 (Mountain Daylight Time) {}**
Everything works fine, but when I send the data to the controller, the JSON automatically converts it to ISO and the date received by the controller is 6 hours in advance.
I understand a little bit the context about GMT-0006 (my current timezone is 6 hours more than the 0 timezone), and the fact that my controllers received the date in ISO format because when I converted to ISO format is the same problem
date.toISOString() = "2021-05-25T23:06:01.861Z" (6 hours in advance)
so my question is, there is a way to create a date that allows me to use .toISOString() and keep the same?
or create a date with my current hour but -0000 so when I convert it to toISOString keeps the same?

Javascript: Date sent to http.put function isn't what gets sent to API

The problem I'm having is that I have a n object that includes a Date property. When the object is created or edited, that Date property is sent to the API using an HTTP put or post method, but the value of the date is changed when it's sent out.
What I see before it gets sent out to the API is this:
Sun Nov 03 2019 07:00:00 GMT-0700 (Pacific Standard Time)
I even put it through a conversion function to reset the time to 00:00:00, so it looks like this:
Sun Nov 03 2019 00:00:00 GMT-0700 (Pacific Daylight Time)
But when I look at the network tab in Chrome the value that is sent for that property is this:
2019-11-03T07:00:00.000Z
Every time it's edited, assuming the date isn't changed, the value of that date is incremented by whatever the offset is (7 hours in this case), so after a couple edits the date changed to the next day... I can't figure out how to keep the property a Date, but make the value sent to the API midnight on the date selected.
I'm sure this has been answered so many times, but I've spent hours trying to find the answer to this and all I can come up with is to convert my date to a string, which isn't possible because the DTO has it set as a Date.
Thanks!

the date in Javascript is swapping between day and month in?

I Have date as a string 10/06/1991 when I need to convert to a date object the day and month value is swiping like this Sun Oct 06 1991 00:00:00 GMT+0200 (GMT+03:00), but if I reverse my string to 1991/06/10 it's correct after converting.
is there any idea how to solve it?
You cannot parse string to date using Date object unless it is in ISO form. Here later date - 1991/06/10 is in in correct order so js can make correct date object.
For your case, you can use any other library like moment for parse your given date 10/06/1991 by giving its format to moment, example is given below -
console.log(moment("10/06/1991", "DD/MM/YYYY").toDate());
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>

format just a time for use with ui-bootstrap timepicker

I have a server returning a time value like so
14:00
The reason for this is it represents the start time of say a task that happens repetively say every monday at 14:00 so the date is irrelevant. I want to display this value to the user when they are editing the task so that they can change it with ui-bootstraps timepicker.
The timepicker requires either an epoch, an rfc2822 or ISO 8601 date so I need to just add a random date that I can discard later for the timepicker to be able parse and display the value (which seems ridiculous seen as it is a time picker not a time on a specific day picker but whatever)
so either using the javascript date object or moment.js how can I take that value and create a valid date object with it?
As you requested a moment.js solution also:
var date = moment("14:00", "HH:mm").toDate();
// assume returned is the hours and minutes you get in this format: 14:00
hourData = returned.split(':');
var date = new Date();
date.setHours(hourData[0]);
date.setMinutes(hourData[1]);
date will be something like Sat Jul 12 2014 14:00:13 GMT+0300 (EEST)

Categories