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

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>

Related

JavaScript Date() parsing time string incorrectly [duplicate]

This question already has answers here:
What are valid Date Time Strings in JavaScript?
(2 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
I have written a helper function to attempt to parse a time string in a flexible way. Sometimes the time comes in as a full date but sometimes its just a string like "10:30" or "12:01 PM".
So I am checking to see if I can parse the date first:
let date = new Date(value);
if (!isNaN(date.getTime()) // its a valid date
If that fails then I fall back to regex to check if I can make any sense of the string manually:
const isValidTime = /^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?(\s?(AM|PM))?$/i.test(value);
While writing unit tests I found some weird results. Incorrect times were returning actual dates in stead of throwing an error. Javascript new Date() is parsing some of the strings and seemingly making up dates for them but also strangely, its getting the hour right too. Here are some examples of time strings that are causing this:
new Date("1:60") // Fri Jan 01 1960 01:00:00 GMT+0200
new Date("2:70") // Thu Jan 01 1970 02:00:00 GMT+0200
new Date("3:80 PM") // Tue Jan 01 1980 15:00:00 GMT+0200
//and a "valid" time for good measure
new Date("5:30") // Invalid Date
What the heck is going on here?

new Date() vs. Utilities.formatDate(new Date()) [duplicate]

This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Google Apps Script date format issue (Utilities.formatDate)
(2 answers)
Closed 2 years ago.
Can anyone tell me why this code would produce two different dates?
let now = new Date(); // today's date (1/2/2021)
Logger.log(now); // Sat Jan 02 09:42:47 GMT-08:00 2021
Logger.log(new Date(now.getTime()-(2*1000*60*60*24))); // Thu Dec 31 09:42:47 GMT-08:00 2020
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY")); // 12/31/2021
Why would Utilities.formateDate() change the date from 12/31/2020 to 12/31/2021?
******** SOLUTION *********
Change the date format from "MM/d/YYYY" to "MM/d/yyyy".
It's just a simple formatting problem. Look here
This:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY"));
Should be this:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/yyyy"));
In other words just make the capital Y's lower case y's and you're there.

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

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"

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