Moment js: Keep UTC date/time in UTC only [duplicate] - javascript

This question already has answers here:
moment js is returning wrong formatted values for an iso timestamp
(2 answers)
Closed 3 years ago.
date = moment(startDate).startOf('day');
date.format('2019-01-01't)
Above code is converting UTC date to local date. How do i keep UTC date UTC?
startDate is a datetime string in iso format

From the moment docs:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc() instead of moment().
So even though your datestring is UTC and moment is correctly parsing the date, it still displays the output in local time unless you use moment.utc(). To display in utc:
const s = '2019-03-08T14:59:40Z';
const date = moment.utc(s).startOf('day').format();
console.log(date);
// 2019-03-08T00:00:00Z
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Related

How to covert date time to local date time using timezone string in JavaScript? [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 12 months ago.
I want to convert given Date time to local Date time, when input Date time and timeZone is given separately as below
Date -20220120080000 ('yyyymmddHHMMSS')
timezone string -("Australia/Perth")
How to convert above timezone to local Date time without using moment library ?
If you are able to use moment, this becomes pretty straightforward.
Check this working example.
let date = "20220120080000";
const format = "YYYYMMDDHHmmss";
let tz = "Australia/Perth";
const remoteTime = moment.tz(date, format, tz);
const localTime = moment(remoteTime).local();

Need to find the difference between two date in seconds [duplicate]

This question already has answers here:
Deprecation warning in Moment.js - Not in a recognized ISO format
(18 answers)
Closed 1 year ago.
I need to find difference between 2 dates in seconds , i am passing one date from front end and another date is current date but i am getting error
here is my code in front end
var a =moment().format('MM/DD/YYYY HH:mm:ss')
here is nodeJs code that I need to compare
var sesdate=moment(request.body.a).format("DD/MM/YYYY HH:mm:ss")
var startDate = moment(sesdate, "DD/MM/YYYY HH:mm:ss");
var currenDate = moment(new Date()).format("DD/MM/YYYY HH:mm:ss");
var endDate = moment(currenDate, "DD/MM/YYYY HH:mm:ss");
var result = 'Diff: ' + endDate.diff(startDate, 'seconds');
but i am getting expected output but getting warning message as
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
If you already have two JavaScript DateTime objects, you can use the .getTime() method to get the milliseconds since 1970. Subtract one from the other and divide by 1000 to convert to seconds. You may need to take the absolute value if it's not guaranteed that one of them is always after the other.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
Alternatively, if you want to stick to your moment code, you'll need to convert your time format to one that is RFC2822 / ISO compatible. The top answer to this question will show you how to do that:
How do I format a date as ISO 8601 in moment.js?

Invalid Date for some UTC Strings Javascript [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:
var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });
Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.
$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019');
Any idea where the Invalid Date issue may come from?
By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.

Converting UTC format from server to local time and getting the time in HH:MM format [duplicate]

This question already has answers here:
How to show current time in JavaScript in the format HH:MM:SS?
(14 answers)
Closed 4 years ago.
I'm converting the utc from the server to local time using javascript and it works.But I need only the time rather than the date+time.Without using moment.
server time(UTC format) : "2018-02-03T10:00:00"
converted local time :2018-02-03T04:30:00.000Z
In the above local time I need only "04:30" alone .Is there any way to get it?
I tried the below one to convert from utc to local time
new Date(response.data.maxDate).toISOString();
console.log(new Date().toLocaleTimeString());
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
And if you want only hours:minutes then you can do this
date=new Date();
date.getHours() +':'+ date.getMinutes()
date=new Date();
console.log(date.getHours()+':'+date.getMinutes());

Javascript getFullYear() returns the wrong year [duplicate]

This question already has answers here:
JavaScript's getDate returns wrong date
(12 answers)
Closed 6 years ago.
This is so basic, but it makes no sense to me:
new Date("2010-01-01").getFullYear();
result: 2009
wth? My goal is to reformat the date as mm/dd/yyyy given the format yyyy-mm-dd..
Adding on:
new Date("2010-01-01").getMonth();
result: 11
new Date("2010-01-01").getDate();
result: 31
The date string you're passing into new Date() has no timezone in it. It's being interpreted as UTC. The critical thing to understand here is that a Date is stored as a Unix timestamp (seconds since 1970-01-01 00:00, making 'Date' a misleading name) so if you don't specify the time within the date, it's going to apply a default.
Date.prototype.getFullYear() retrieves the full year for that timestamp in your LOCAL time. (See the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear)
You're somewhere west of UTC, and 2010-01-01 UTC is 2009-12-31 in your local time.
And for your final mystery....getMonth() is 0-based, not 1-based, so '11' is December.
Don't use the Date constructor to parse strings, it's largely implementation dependent and inconsistent.
An ISO 8601 date and time without a timezone like "2016-02-29T12:00:00" should be treated as local (i.e. use the host system timezone offset to create a Date), but a date–only string is treated like "2016-02-29" as UTC. The first behaviour is consistent with ISO 8601, but the second isn't.
Some versions of browsers will treat date–only strings as UTC, and some as invalid dates, so always parse strings manually (a two line function or library can help). That way you know how it will be parsed in all hosts.
Provide Time with date in the new Date() as parameter . Then u will get exact Result.

Categories

Resources