How to display timezone using momentjs [duplicate] - javascript

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

Related

Why new Date(yyyy-mm-dd) shows past date? [duplicate]

This question already has answers here:
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 3 years ago.
why its showing past day date
var date = new Date('2020-01-01');
console.log(date)
Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)
Because of your timezone settings (Easter Standard time is GMT -0500 therefore 5 hours before 2020-01-01).
Javascript Date object are timestamps - they merely contain a number
of milliseconds since the epoch. There is no timezone info in a Date
object. Which calendar date (day, minutes, seconds) this timestamp
represents is a matter of the interpretation (one of to...String
methods).
Basically it is the toString method that converts the date to your local timezone.
Source

How do I convert ISO date format to yyyy-MM-dd format using momentjs or vanilla JS? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I'm trying to hit a soccer sports API which includes date in the format of yyyy-mm-dd, only the scores from that date to current date will be displayed. The current date is chosen by user using a calendar but when the user chooses the date from calendar, it gets displayed in ISO format as "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" . I want to convert this date in the front end in the yyyy-mm-dd format and send it to the API Url in back end. I'm using AngularJS and Java. How do I convert the full ISO date into that format?
Based on that output it sounds like your date is stored as a JavaScript date object (see: https://www.w3schools.com/js/js_dates.asp)
To get the string you want, one solution would be to take the value of your input (I'll call it d) and do the following (I assume you have momentjs loaded:
var datestring = moment(d).format('YYYY-MM-DD')
datestring should now include the date in the format you want... if for some reason d is a string instead of a date object, you can create a parsing pattern following the momentjs doc here: https://momentjs.com/docs/#/parsing/
Assuming you have a JavaScript Date object to work with, you can do this in plain JS:
var datestring = dateobj.toISOString().substring(0, 10); // 'yyyy-MM-dd'
If you only have the display string ("Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)"), you can first convert that into a Date object with this:
// displaystring = "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)";
var dateobj = new Date(displaystring);
...and then do the datestring conversion above.

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

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