How can we change the date format in JavaScript? [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Format a date string in javascript
(7 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
Given the data 2018-04-28T18:38:23Z
When I use Date(2018-04-28T18:38:23Z)
This gives me Sat Apr 28 2018 18:38:23 GMT-0500
I want to remove that GMT thing, but how can I do with simply without
writing some long code to parse it out?

JavaScript Date has some methods you could use to craft the String you want.
Here's something close, you could play around with the methods (getHours/Minutes/Seconds), but if you want exactly that without the timezone, I would recommend parsing out or doing some String manipulation:
var d = new Date();
console.log(d.toDateString() + " " + d.toLocaleTimeString());
// Output:
// Sat Apr 28 2018 4:58:05 PM

Related

i want to convert date Data to date? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Converting dates in JavaScript
(5 answers)
Closed 2 years ago.
I get data from open weather api and when i convert it i got the full date i want to extract only the hours :
sunrise: 1607412091
i try this method
var daterise= data.sys.sunrise;
var exactdate= new Date(daterise*1000);
document.getElementById("rise").innerHTML="Sunrise:"+ exactdate;
and i got the full date
Tue Dec 08 2020 08:21:31 GMT+0100 (UTC+01:00)
I want just the hour
i want to extract the hours from the date ??
cosnt hours = exactdate.getHours()

Convert date to format yyyy-MM-dd+timezone [duplicate]

This question already has answers here:
how to format date in Component of angular 5
(4 answers)
Closed 3 years ago.
I wanna change the input date with time and time zone to this format yyyy-MM-dd+timezone
Example:
I have this input
Sat Oct 05 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
I want an output like this
2019-10-05+2:00
Is there a simple way to convert this?
You can do this with simple Date functions.
public transform(date: Date): string {
return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate() >= 10 ? date.getDate() : '0'+date.getDate()}${date.getTimezoneOffset()}`;
}

Javascript Date() returns wrong date [duplicate]

This question already has answers here:
javascript Date timezone issue
(3 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I've been looking for someone with the same problem here but I could not find. Whenever I call Date() on my hidden value it seems to be subtracting one day. I am using Chrome.
I am passing a hidden value to my html:
<input type="hidden" name="start_date" value="2018-07-29" class="start-date" id="id_start_date">
It's correct when I call:
var hiddenDate = $('#id_start_date')[0].value;
alert(hiddenDate); # 2018-07-29
But incorrect if I call:
var date = new Date(hiddenDate);
alert(date); # Sat Jul 28 2018 19:00:00 GMT-0500 (Central Daylight Time)
What am I doing wrong and how can I fix it? Thanks

Javascript Change timestap format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Convert PHP Unix Timestamp to Javascript Timestamp format
(1 answer)
Closed 6 years ago.
I initialize my timestamp variable like
var current_time = new Date();
My output is in
Tue Oct 11 2016 15:09:04 GMT+0800 (Malay Peninsula Standard Time)
format. How can I change it to 2016-10-11 07:19:48pm format?
you can acheive this using moment.js lib,
just run
moment().format('YYYY-MM-DD hh:mm:SS A'); // print 2016-10-11 03:20:88 PM

Format a javascript date like 20130511T0825 [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
Just wondering how I would format a javascript date to be like
yearmmddThhmm
20130511T0825
The best thing I've found for formatting dates in JS (aside from painstakingly putting the pieces together) is using momentjs. It is lightweight and the API is super simple. Just give it a date object, pass it a few parameters, and bam, get your nicely formatted date back.
Date has toISOString, which is almost there, then just strip out what you don't want.
var d = new Date(), // Tue Jun 04 2013 21:23:52 GMT+0100 (GMT Daylight Time)
s = d.toISOString(); // "2013-06-04T20:23:52.058Z"
s.replace(/[^\da-z]/ig, '').slice(0, -6); // 20130604T2023"

Categories