String "conversation" date format to date format in JavaScript [duplicate] - javascript

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 1 year ago.
I am creating a Speech to Text input engine where user will say input values for html forms. One of the input field is a type="date". Suppose the user says "July 20th 2021", Is it possible to change this date into JavaScript date format? Is there any library that does this kind of conversion?
Thanks in advance.

I would start with the Date.parse() function. If that doesn't work for you then your best bets are likely date-fns or momentJS but if your format isn't one that is immediately parseable with one of the built in functions then you may need to a bit of pre-processing yourself.

Related

Moment.js retrieving a different date when using toDate() and format() [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I need to get the current date in my TypeScript application. It is now 13:35 for me.
When using moment(Date.now()).toDate() I get the current date, but an hour too early than what I would expect:
"2020-03-26T12:35:12.938Z"
This gives me 12:35.
When using moment(Date.now()).format("DDMMYYYY_HHmm") I get the current date with the hour I am expecting: "26032020_1335"
This gives me 13:35.
What am I missing here to get the correct date and time?
To be clear, i need a Date object. Not a string.
When you call moment(Date.now()).toDate(), I presume you logged that date to the console. When a date is logged to the console, it displays as an ISO 8601 date, which uses UTC time. So, it is the correct time, just in a different timezone. If you call .toString on the date you should see the correct time for your timezone.

How to Convert datetime from one timezone to another timezone in Javascript? [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 3 years ago.
I need to convert from one timezone to another timezone in my project.
I am able to convert from my current timezone to another but not from a different timezone to another.
For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.
However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.
The easy and fast way is to use an external library. I recommend moment.js. once installed you can convert pretty easily and reliably through their ready-made functions

How to convert a Date to OLE Automation Date in javascript [duplicate]

This question already has answers here:
What is equivalent of DateTime.ToOADate() in javascript?
(7 answers)
Closed 4 years ago.
I need to pass a Date value to a .Net server which expect to receive OLE Automation Date value (Microsoft Timestamp - a decimal value).
Any idea how to convert a Javascript Date to this format in JS?
Thanks in advance.
You can use momentjs but I don't think that's a good practice. I think it is better to pass an standard date format (like ISO) and then in your .NET application convert it to OLE Automation Date format, this way the front part is not coupled to your database.

Convert Javascript date to shorter date time and also UTC [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I have a JavaScript Date which I want to get it into SQL Server datetime field.
The JavaScript time sent over as
Fri Sep 15 2017 00:11:44 GMT-0700 (US Mountain Standard Time)
I want it to be converted in javascript or in C#
I need the date to be like
2017-09-15 23:47:01
OR perhaps
9/15/2017 11:47:01 PM
I need to also convert it to UTC , not sure if that is easier to do in javascript or in C#
Which type of format is my code even in? "Fri Sep 15 .." ??
For UTC I was seeing code like this
var isoDate = new Date('yourdatehere').toISOString();
( I am doing .net core web api, with Angular 4 / Typescript)
As Keith said, working with dates is really hard in javascript, especially considering very different implementations across the browsers. So I will second their opinion: just use moment.js and leave everything to that library. It is really powerful. Using it you can get any date format you can possibly think of. To get "2017-09-15 23:47:01" all you need is the following (please pay attention that qualifiers are case sensitive!):
var fmt = moment(<your_js_date_if_you_have_it>).format("YYYY-MM-DD HH:mm:ss");
You can find all supported qualifiers here.
Apart from formatting this library also allows you to do a lot of manipulations with the dates, compare them, translate dates from local to UTC time zone and back, etc.

Convert to datetime to local timezone javascript [duplicate]

This question already has answers here:
How to convert Moment.js date to users local timezone?
(5 answers)
Closed 6 years ago.
I am building an app using node.js + postgresql. My postgresql stores dates in the format:
2016-04-01T04:00:00.000Z
When the date is returned to the browser, I want the date to be returned in the timezone of the user. Is there a way I can do this? I found moment.js but I'm not sure if it actually detects a user/browser's timezone for the conversion or not...
Can someone help? Thanks in advance!
If you want suport multitimezone, you should have in db unix timestamps.
Moment js Is really good lib for an time calculation.
Moment js can detect browser timezone.
Of course you always use vanilia like
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Categories

Resources