Format a javascript date like 20130511T0825 [duplicate] - javascript

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"

Related

how to convert date object to string using javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 11 months ago.
I have a date object as follows
let dateObj = Sat May 01 2021 20:21:00 GMT-0700 (Pacific Daylight Time)
I want to convert above value in this string format.
let stringVal = 2021-05-01T20:21:00.000+0000
can someone let me know how to achieve this. i Have tried toUTCString(), to DateString(), toISOString() and all other methods but i was not able to achieve the result in the format above.
Any guidance is appreciated. is it even possible ?
Not sure if you wanted it converted to UTC, but based on your example it looks like not.
Maybe something like this? Just replace "new Date()" with your date object.
const date = new Date(new Date() - 1000 * 60 * 60 * 7); // subtract 7 hours since .toISOString() is going to add 7 hours
console.log(date.toISOString().replace('Z','+0000'));
* I should add this assumes the client's timezone is Pacific Daylight Time, or a least something with the same offset

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

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

Display Date in the way JavaScript use it? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
If I post there, it's after research that didn't get answers. Here's my problem :
I would like to display on the html the Date() in the format JavaScript use it. If I run this : something.innerHTML = new Date(); I'll obtain "Mon Mar 05 2018 18:13:35 GMT+0100 (Paris, Madrid)". However if I run this : console.log(new Date()); It will give me "2018-03-05T17:15:05.795Z" which is how JavaScript use it in his execution (If I'm right) and what I want to get in the html.
Hopping it was understandable, to resume : how to get "2018-03-05T17:15:05.795Z" (used by js in execution) displaying on html instead of "Mon Mar 05 2018 18:13:35 GMT+0100 (Paris, Madrid)" ?
Thank you very much for any answers, have a nice day :)
- GreenData
You can just use toISOString() like:
something.innerHTML = new Date().toISOString();
You can try toISOString()
document.getElementById('showDate').innerHTML = new Date().toISOString();
<div id="showDate"></div>

Why does new Date(dateString) return two different dates on different devices with exactly the same input? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
Knowing that my timezone is GMT+2, consider the following code:
Running On a Selfy 4G phone:
myDate = "2017-05-12T09:00:00";
dateFoo = new Date(myDate); // Fri May 12 2017 11:00:00 GMT+0200 (CEST)
Running on a Galaxy S7:
myDate = "2017-05-12T09:00:00";
dateFoo = new Date(myDate); // Fri May 12 2017 09:00:00 GMT+0200 (CEST)
Why is there an inconsistency in the outputs and how would I go about solving it?
My question is different from other similar questions (such as Why does Date.parse give incorrect results?) because in my case I am using the exact same string and it's the devices that differ.
The initial problem was that Date.parse on one device took my local time as the time zone, whereas on the other device it took UTC.
By appending a Z at the end of my initial dateString, I forced the date to always be considered as UTC no matter what the device, therefore achieving consistent results with Date.parse().
In order to then get the date in my local time, I used the answer to this question: https://stackoverflow.com/a/1486612/1875581.
Diff in your date is because of timezone.
You can try to convert date to UTC date for get perfect result like this.
myDate = "2017-05-12T09:00:00";
dateFoo = new Date(myDate).toUTCString();

Format javascript current date and time [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I display a date/time in the user's locale format and time offset?
Hi - simple question - I just want to take this:
document.getElementById("time").innerHTML= new Date();
and format it into something legible, like this:
May 18, 2011 7:45 AM
making sure it is localized to whomever might be seeing it. Currently, it prints out as this:
Wed May 18 2011 07:46:25 GMT-0400 (EDT)
How do I do this?
Steven Levithan's dateFormat() (only 1.2KB when minified and gziped!) should do just what you need.
Javascript
// Formatting in: May 18, 2011 7:45 AM
var formattedDate = new Date().format('mmm dd, yyyy h:mm TT');
document.getElementById("time").innerHTML= formattedDate;
Look up the reference for
Date.toLocaleString()
Date.toLocaleDateString(), and
Date.toLocaleTimeString().

Categories