Convert string to date without considering timezone [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
From service I get date string in format 'yyyy-mm-dd' (eb, '2017-09-14')
In JavaScript, I convert this string to Date like as mentioned below:
new Date('2017-09-14')
It works just fine. But I am facing problem in different time zones. For example
In EST Timezone: Wed Sep 13 2017 20:00:00 GMT-0400 (Eastern Daylight Time) and
In IST Timezone: Thu Sep 14 2017 05:30:00 GMT+0530 (India Standard Time)
Is there a way to convert it to the same date e.g. Sep 14, 2017

YOu can do it like this
var d=new Date(date value);
var actualDate=d.getDate();
var actualMonth=d.getMonth()+1;
var actualYear=d.getFullYear();
var formattedDate=actualDate + '/' + actualMonth + '/' + actualYear;
You can save it to any format

Related

Can you create a dateString from returned values from getFullYear, getMonth, getDate [duplicate]

This question already has answers here:
Javascript date format like ISO but local
(12 answers)
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 4 months ago.
I have a date like this
Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
I am running this function on this date
const date = Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())
Which returns
2022 9 29 2 0 0
Is it possible to convert these numbers to
2022-10-29T02:00:00.000
the whole point of this is to use a library like moment-timezone and parse 2022-10-29T02:00:00.000 to a new timezone for example new_york. Im doing this to get the UTC time for 2022-10-29T02:00:00.000 in new york. The issue with new Date(2022-10-29T02:00:00.000) is that it always converts it to my local timezone.

Get timestamp with milliseconds in Javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I want to get the current date including the milliseconds. I know that Date.now() is returning the Unix epoch time in milliseconds, but the object Date contains only the Date and timestamp with second precision.
console.log("Time: " + new Date);
Time: Tue Apr 14 2020 12:21:26 GMT+0200 (Central European Summer Time)
But I want it to look like this:
Time: Tue Apr 14 2020 12:21:26.500 GMT+0200 (Central European Summer Time)
Is there a way to change the format in order to display the milliseconds as well or do I need to concatenate the Date string with new Date().getMilliseconds();?
You can use moment.js to achieve the format you need, and the other option is to concatenate manually
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SSS'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script>

How can I format date to string in javascript? [duplicate]

This question already has answers here:
Javascript - reformat date string to ISO8601
(2 answers)
Closed 3 years ago.
How can i format date in JS given in this format: Mon Jan 08 1996 00:00:00 GMT+0100 to string like this: "1996-01-06T23:00:00.000Z" ?
var str = "Mon Jan 08 1996 00:00:00 GMT+0100";
var date = new Date(str);
console.log(date.toISOString());
Make the string a date object, then convert it to ISO format. That's all there is.
use the following :
var event = new Date('Mon Jan 08 1996 00:00:00 GMT+0100');
console.log(event.toISOString());
First convert Mon Jan 08 1996 00:00:00 GMT+0100 to Date using new Date() function.
Second convert the new date into ISO format using toISO() function.

Getting `toISOString` string with 'GMT` Details [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am looking for a date output like : "2019-05-29T06:33:33.537+05:30" - which includes gmt value. I tried to get the same by using:
var newFormat = new Date();
console.log(newFormat.toISOString());
But getting value as : 2019-06-16T03:00:27.989Z -not finds the GMT value added. What is the correct approach to get the required value?
Thanks in advance.
You can use toString():
var newFormat = new Date();
console.log(newFormat.toString());
toString gives you Wed Jan 23 2019 17:23:42 GMT+0800 (Singapore
Standard Time).
toDateString gives you Wed Jan 23 2019.
toLocaleString gives you 23/01/2019, 17:23:42.
toLocaleDateString gives you 23/01/2019.
toGMTString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toUTCString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toISOString gives you 2019-01-23T09:23:42.079Z.

HTML input date is being read back "incorrectly" [duplicate]

This question already has an answer here:
Initialize JS date object from HTML date picker: incorrect date returned
(1 answer)
Closed 3 years ago.
When I take in 3/15/2019 as the input date and then read back the time with console.log(fromDate) I get Thu Mar 14 2019 19:00:00 GMT-0500 (Central Daylight Time).
Why is it not Fri Mar 15 2019 00:00:00 ?
Is it because of the time on my local computer?
Is there a way to achieve the desired output?
fromDate = new Date(document.getElementById("fromDate").value);
This happens because new Date consider the date as UTC, so
var date = new Date('3/15/2019')
creates the date Fri Mar 15 2019 00:00:00. When you print the string however it considers your actual timezone, which is GTM-0500, so it removes 5 hours from the original date, yielding Thu Mar 14 2019 19:00:00.

Categories

Resources