How to understand zone by MOMENT.js - javascript

I have problem with String > Format of timezone.
I have string: " 2015-02-10 00:00:00,3,UTC "
And try to format it in moment:
moment('2015-02-10 00:00:00,3,UTC', 'YYYY-MM-DD HH:mm:ss, ?, ?')
What should I insert instead of "?"

It's not possible to format that kind of string into moment because a lone 3 does not designate the timezone offset in any standard format.
You need to change the 3 into +0030.
This should work:
var date = '2015-02-10 00:00:00,3,UTC'
.replace(/,(\d\d),/,',+$100,') // for double digit cases (11 turns to +1100)
.replace(/,(\d),/,',+0$100,'); // single digit cases (3 to +0300)
And then
moment(date, 'YYYY-MM-DD HH:mm:ss,ZZ')
I'm not sure what the UTC part is about since +0300 is clearly not UTC. I guess it's just saying that the the 3 hour offset is relative to UTC?

Related

transforming a date in Month day, YEAR format to YYYY-MM-DD format?

Is there a way to reformat a date using .replace and a regex? or a series of them?
for example I would like to turn May 4, 1981 into 1981-May-04... this would be good enough for my needs.
but even better would be to turn May 4, 1981 into 1981-05-04.
please note that single digit months and dates need to be changed to double digit (i.e. prefix a 0). The source text May 4, 1981 would not contain a leading 0 for the day ever.
the end result YYYY-MM-DD being sortable, which is why the leading 0 is important.
new Date('May 4, 1981').toLocaleDateString('en-CA')
outputs
'1981-05-04'
toLocaleDateString is pretty powerful - check it out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
Alternatives:
Write your own simple function to extract the date data, irrespective of the locale.
const date = new Date('May 4, 1981');
function getMyFormat(date) {
return [
date.getFullYear(),
date.getMonth().toString().padStart(2, '0'),
date.getDay().toString().padStart(2, '0')
].join('-');
}
console.log(getMyFormat(date))
Use the ISO string (standardized format of time representation here)
new Date(date).toISOString().split('T')[0]
It spits out a long string where, before the 'T', the format is YYYY-MM-DD.
There are some caveats to this, however, as it puts the date in the UTC timezone, which could affect your setup.
There is, however, an easy way to handle this:
new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000).toISOString().split('T')[0]
Over here, we calculate the timezone offset with respect to UTC so it should help you get the format you need.

Moment converting digit into date

Trying to understand how moment.js is converting a string to date, I've bounced into this issue.
let date = "User has logged in to more than 10 .";
console.log(moment(date)); //output date
let invalid = "User has logged in to more than 10 a";
console.log(moment(invalid)); //output invalid date
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js
"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/2.2.0/moment-range.js"></script>
can someone explain it to me ??
CodePen link
When you pass the string moment checks whether it is a valid date format, and if not, it falls back to the built-in javascript Date.parse() method.
The moment.js docs say:
When creating a moment from a string, we first check if the string
matches known ISO 8601 formats, we then check if the string matches
the RFC 2822 Date time format before dropping to the fall back of new
Date(string) if a known format is not found.
Date.parse does not recognize anything useful in your string until it encounters 10; it drops the rest. A default date format is assumed, which will depend on your location and language. In my own case, here in the US, the format is MM/DD. The result is that the string is parsed to a date of Oct. 1st (10th month, no day specified defaults to the 1st). And then (for Y2K-ish reasons, I suspect) it assumes a year of 2001, since no year is given.
We get the same behavior from javascript's built-in Date methods:
new Date(Date.parse('User has logged in to more than 10.'))
// Mon Oct 01 2001 00:00:00 GMT-0400 (EDT) <- As printed from Michigan.
In your second case, you tried ending the string with 10 a instead of 10 . and you will notice the same behavior (invalid date) if you pass the same to the built-in Date methods.

Why does adding a space character at the end of a year string cause JavaScript (node) to apply the timezone offset?

This is in Node v8.9.0 specifically.
Consider these lines:
console.log(new Date("2006"));
console.log(new Date("2006 "));
They produce this output:
2006-01-01T00:00:00.000Z
2006-01-01T08:00:00.000Z
Note that the second line has an 8hr timezone offset.
Why would the presence or absence of a trailing space cause the date to be created with/without the timezone offset?
Summary: The presence of the space causes the date parser to interpret the date as a different format which uses a different assumption for the time zone when there is no timezone specification in the string.
A complication with the Date() constructor or Date.parse() is that with incomplete or partial date strings the parser is trying to guess whether the string should be interpreted as a simplified form of the ISO 8601 format or the RFC2822 format. If it thinks the format is ISO 8601 and no timezone is specified, then the UTC time zone will be assumed. If it thinks the format is RFC2822, then the local computer time zone will be assumed.
So, for your two strings the first is apparently being assumed to be ISO 8601 and thus uses UTC time zone assumption and the second is apparently being assumed to be RFC2822 which uses the local time zone.
The ISO 8601 format that is supported contains no spaces, whereas the RFC2822 format can contain some spaces so it seems likely that the mere presence of the space in the string causes the parser to choose the RFC2822 format which uses the local time zone.
You can read about some of this in the Date.parse() doc on MDN.
For reference here's a piece of the simplified ISO 8601 format:
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
T = a literal T separating the time
You can see that there are no spaces in this format.
Whereas the RFC2822 format contains spaces separate different pieces of the date/time. The full grammar is in the linked RFC, but here's one example where you can see the spaces:
Mon, 25 Dec 1995 13:30:00 GMT
Note that the ES2015 spec for Date.parse() describes the simplified ISO 8601 date format. When you supply any string that does not match that format exactly you probably run into some implementation dependent parsing behavior. Quoting from the ES2015 spec:
the value produced by Date.parse is implementation-dependent when
given any String value that does not conform to the Date Time String
Format (20.3.1.16)
where that referenced "Date Time String Format" is the simplified ISO 8601 format.

How to get date & time using moment.js

I need to get the start date and end date in the below format using moment js
startDate = 20160427000000 and
endDate = 20160427235959
Here the start date appended with 000000 and end date appended with 235959
What is the right way to get this result in javascript
You want the format operator. Since it looks like your 0's and 2359's are hardcoded (I assume you're doing start and end of days), try:
startDate = moment().format('YMMDD000000');
endDate = moment().format('YMMDD235959');
EDIT: Or, as RobG pointed out, you can use:
startDate = moment().startOf('day').format("YMMDDHHmmss");
endDate = moment().endOf('day').format("YMMDDHHmmss");
(Which is much neater)
I'm totally confused, I don't know if you want to parse the format or output it. If you want to parse dates using moment.js in that format, then in time zone +05:30:
// Format YYYYMMDDHHmmss for 2016-04-26T00:00:00
var s = '20160426000000';
var x = moment(s, 'YYYYMMDDHHmmss');
// Show date in ISO 8601 extended format
console.log(x.format()); // 2016-04-26T00:00:00+05:30
To shift to the end of the day and output in YYYMMDDHHmmss format:
console.log(x.endOf('day').format('YYYYMMDDHHmmss')); // 20160426235959
In the format string:
YYYY is 4 digit year
MM is two digit month
DD is two digit day
HH is two digit hour in 24 hour format
mm is two digit minute
ss is two digit seconds
For Getting Format Like these in moment.js - (2020-12-15T13:00:00)
let a =2023-01-14T20:15:00-05:00
You can use moment(a).format("YYYY-MM-DDTHH:mm:ss")
Result: 2023-01-14T20:15:00

Javascript Date string constructing wrong date [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
Hi I am trying to construct a javascript date object with a string, but it keeps contructing the wrong day. It always constructs a day that is one day behind. Here is my code
var date = new Date('2006-05-17');
The date i want to get is
Wednesday May 17 2006 00:00:00 GMT-0700 (PDT)
But instead I get
Tue May 16 2006 17:00:00 GMT-0700 (PDT)
When you pass dates as a string, the implementation is browser specific. Most browsers interpret the dashes to mean that the time is in UTC. If you have a negative offset from UTC (which you do), it will appear on the previous local day.
If you want local dates, then try using slashes instead, like this:
var date = new Date('2006/05/17');
Of course, if you don't have to parse from a string, you can pass individual numeric parameters instead, just be aware that months are zero-based when passed numerically.
var date = new Date(2006,4,17);
However, if you have strings, and you want consistency in how those strings are parsed into dates, then use moment.js.
var m = moment('2006-05-17','YYYY-MM-DD');
m.format(); // or any of the other output functions
What actually happens is that the parser is interpreting your dashes as the START of an ISO-8601 string in the format "YYYY-MM-DDTHH:mm:ss.sssZ", which is in UTC time by default (hence the trailing 'Z').
You can produce such dates by using the "toISOString()" date function as well.
http://www.w3schools.com/jsref/jsref_toisostring.asp
In Chrome (doesn't work in IE 10-) if you add " 00:00" or " 00:00:00" to your date (no 'T'), then it wouldn't be UTC anymore, regardless of the dashes. ;)
Remove the prepending zero from "05"

Categories

Resources