Change timezone format - javascript

after chaning my date timezone using toLocalString()
new Date(dateParam).toLocaleString('en-US', { timeZone: "Australia/Sydney" })
i've got this string 5/25/2022, 8:44:46 PM, how can i transform from this format to default new Date() fromat Tue Jul 05 2022 23:22:37 GMT+0300, to be able to use Date methods(example: toDateString(), getFullYear())?

add to your solution two parameters dataStyle and timeStyle like in the example below
console.log(new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long' }).format(date));
// Expected output "Sunday, 20 December 2020 at 14:23:16 GMT+11"

Related

How to change ISO date to Standard JS Date?

I am trying to change an ISO date to a Standard JS Date format. The JS format I am referring to is:
Mon `Jul 20 2020 14:29:52 GMT-0500 (Central Daylight Time)`
What is the best way to go about doing this? Thanks!
const ISO_DATE = '2020-07-14T23:02:27.713Z';
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toString();
};
console.log(formatDate(ISO_DATE));
One way is:
let isoDate = "2020-07-20T14:29:52Z";
var myDate = new Date(isoDate);
console.log(myDate.toString()); // Mon Jul 20 2020 17:29:52 GMT+0300 ( (your time zone)
console.log("Back to ISO Date: ", myDate .toISOString());
If you want to convert it back to ISO Date use:
console.log(myDate.toISOString());

How to convert a Date object to output only hh:mm am/pm

I am attempting to convert the following into a 12 hour am/pm format.
Currently I am recieving the Day, Month, Year and timezone.
Fixed by adding .toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.)/, "$1$3")*
<div id="time1"></div>
<div id="time2"></div>
var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;
var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;
Basically what you have to do is use the Date() default javascript function and make sure you append the UTC timezone:
var date = new Date('08/16/2019 7:00:00 PM UTC')
date.toString=() //will then print out the timezone adjusted time
"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"
There are many built in javascript methods to handle converting date objects. This example will look to the browser to determine date format and time.
let time = Date.now();
time.toLocaleDateString();

Javascript Date is not formatting correctly using `toLocaleString`

I am trying to format date in 'MM/dd/yyyy' (short date format) using toLocaleString function of javascript Date. But it is not giving expected result when I change my timezone. Bellow is snippet of my code
var d1 = new Date(1954, 0, 1); // Fri Jan 01 1954 00:00:00 GMT-0900 (AKST)
var options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
var shortDate = d1.toLocaleString('en-US', options);
console.log(shortDate);
Above code prints 12/31/1953 instead of expected result which is 01/01/1954.
Observations/Steps:
Change timezone of local machine to Whitehorse - Canada(Pacific DayLight Time).
Run above code snippet to replicate result.
It only gets reproduced for year value less than 1968 (works fine for years greater than 1968)
For reference, uploaded recording for it (http://recordit.co/cdWMgWFLKJ)
local date time function is a conversion from UTC to locale date time. You need to change UTC time first.
so use this.
var d1 = new Date(Date.UTC(1954, 0, 1)); // Fri Jan 01 1954 00:00:00 GMT-0900 (AKST)
var options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
var shortDate = d1.toLocaleString('en-US', options);
console.log(shortDate);

momentJS convert possibility day into Date() js format

I am trying to convert a date in format momentjs into a date from javascript native new Date().
The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.
myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...
It is possible to convert it like that way?
Don't need moment:
let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"
you want something like this:
moment(myDay, "YYYY-MM-DD").toString();
moment().toString() Returns an english string in a similar format to JS Date's .toString().
moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"

How to format date without time using toLocaleTimeString

I have a Date, which I want to convert to a string, but without the time, just the date part.
My current code:
var date = new Date(2014, 9, 08); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleTimeString("en-US", options));
// output: Wednesday, October 8, 2014 12:00:00 AM
// what I'm looking for: Wednesday, October 8, 2014
How can I modify the options to not display time?
Juste use toLocaleDateString instead of toLocaleTimeString and you should get the result you are expecting :
var date = new Date('2014', '9', '08'); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleDateString("en-US", options));
returns : "Wednesday, October 8, 2014"
I will also second the above poster and recommend using moment.js; it is lightweight and very flexible.
If you are satisfied with allowing the browser to determine the display format, you can use the toDateString or toLocaleDateString functions of the Date object. You will get different results on different browsers.
Otherwise, consider using a library such as moment.js, so you can control the format to your liking. For example:
moment([2014,9,8]).format('dddd, MMMM Do, YYYY') // "Wednesday, October 8, 2014"

Categories

Resources