Format javascript current date and time [duplicate] - javascript

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().

Related

new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy') [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Closed 4 years ago.
Is it possible to enter date in dd/mm/yyyy format into newDate (e.g. 03/01/2018) so that it returns object Wed Jan 03 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {}?
If I have a date 03/01/2018 it returns Thu Mar 01 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {} instead... I'm not trying to pass mm/dd/yyyy format..
I've looked at plenty of posts on this and can't find an answer to my question.
You have no control over the Date constructor, so you need to feed it a date in the format that it wants. Since you are formatting the date yourself, it is better to use the other Date constructor, which takes the year, monthIndex, and day as arguments, since it is more bullet-proof across different browsers and runtimes:
function my_date(date_string) {
var date_components = date_string.split("/");
var day = date_components[0];
var month = date_components[1];
var year = date_components[2];
return new Date(year, month - 1, day);
}
console.log(my_date("03/01/2018"));
The case of dates one area where I install the moment library on nearly every JavaScript project I create.
Note: Snippet may display the result differently; check your console.
You could use regex like so:
var date = new Date("03-01-2018".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"))
I suggest you use momentjs from http://momentjs.com/ . it is very easy to use to output any format u want.
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2018, 10:30:09 pm
moment().format('dddd'); // Thursday
moment().format("MMM Do YY"); // Jun 28th 18
moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018
moment().format();

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>

How to display timezone using momentjs [duplicate]

This question already has answers here:
Format datetime with moment.js to show timezone
(2 answers)
Closed 5 years ago.
I am trying to display a date like this, with current timezone
Jul 20, 2017 2:39pm EDT
What I manage to get is this
<Moment format="MMM DD, YYYY h:mma">{new Date()}</Moment>
Output: Jul 20, 2017 2:39pm
I also found this
<Moment>{new Date()}</Moment>
Output: Thu Jul 20 2017 15:16:50 GMT-0400
But I don't want all the extra stuff, I just to display timezone name.
The general problem is that time zone abbreviations are not available from the browser through a consistent API. In order to provide them, one has to have an external source of data.
-There is also now built-in support for time zone detection/guessing in momentjs timezone:
var tzName = moment.tz.guess();
var abbr = m.tz(tzName).zoneAbbr(); // or .format('z')
More importantly, you can find other options in this post Get timezone abbreviation using offset value

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 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