d.getTime().toString().search(/Wed/i)
I don't get it... typeof returns string, and if i copy and paste "Wed Jul 14 2010 15:35:53 GMT-0700 (PST)" and save it to the var str and do str.search(/Wed/i) it returns 0 but when i do it like above i always get -1, even tho, as i said, it returns a string typeof.
Any ideas how to check if Wed is in that str?
Just for reference, i'm looping through 7 days, checking for Wed, if it's wed, i save the current date and break out of the loop. If you know a better way let me know. Right now im just doing a while(x<=6)
getTime on a Date returns the number of milliseconds since 1 January 1970, so won't contain the string 'Wed'.
Perhaps you meant d.toString().search(/Wed/i) instead?
If d is an instance of Date, then a better way to check if it is a Wednesday would be to test if the result of getDay is 3:
d.getDay() == 3
The reason it returns -1 is that "Wed" will never appear in your string, because "getTime()" returns a big number: the number of milliseconds since the epoch.
Calling "toString()" on that big number still returns a big number, with the digits formatted as a string, as in "1278975122089". It does NOT return the date and time, as in "Mon Jul 12 15:49:59 PDT 2010".
The getTime() method returns the
number of milliseconds since midnight
of January 1, 1970 and the specified
date.
Try using the following instead, without the getTime() call:
d.toString().search(/Wed/i)
Related
I'm honestly not sure how to phrase this question. Basically open a JavaScript console (node, your browser or wherever) and try this:
Date(564018060878018050) // 'Fri Nov 23 2018 06:22:20 GMT-0800 (Pacific Standard Time)'
new Date(564018060878018050) // <-- Invalid Date
I have no idea why the first one works and the second one doesn't. Is there another way to parse. I'm trying to stay away from using a library for this.
The specs says that:
The actual range of times supported by ECMAScript Date objects is
[...] exactly –100,000,000 days to 100,000,000 days
measured relative to midnight at the beginning of 01 January, 1970
UTC. This gives a range of 8,640,000,000,000,000 milliseconds to
either side of 01 January, 1970 UTC.
The valid range is much smaller than the value you used (564,018,060,878,018,050).
And deep inside the Date(value) constructor we have:
If abs(time) > 8.64 × 1015, return NaN.
This explains why new Date(564018060878018050) yields invalid date.
As for Date(564018060878018050) the specs say that:
... Invoking a constructor without using new has consequences that
depend on the constructor. For example, Date() produces a string
representation of the current date and time rather than an object.
So Date(value) is supposed to return current date as a string and not a date.
> Date(564018060878018050) === (new Date()).toString()
< true
> typeof Date(564018060878018050)
< "string"
You are calling the Date constructor as Function and as say in ECMAscript doc:
"When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC)."
"NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments."
You can find more details here: https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.2
I am wondering why those two Date objects have different output in console. In my opinion it should be the same but I can be wrong :)
var twoLinesSetup = new Date();
twoLinesSetup.setHours(0, 0, 0);
var inlineSetup = new Date().setHours(0, 0, 0)
console.log('twoLinesSetup', twoLinesSetup);
console.log('inlineSetup', inlineSetup);
And the console
twoLinesSetup: Mon May 08 2017 00:00:00 GMT+0200
inlineSetup :1494194400521
Why is it so?
twoLinesSetup contains the return value from instantiating the Date constructor, which returns a new Date instance object.
inlineSetup contains the return value from the setHours method, which returns the number of milliseconds since 1 January 1970 00:00:00 UTC
inlineSetup stores the return value of setHours. It is "A Number, representing the number of milliseconds between the date object and midnight January 1 1970"
twoLinesSetup is an actual object, which you do manipulate. The console output - the "toString" method if you want it like that, is the formatted string representation you see.
In theory you should see the same number by doing twoLinesSetup.getTime().
You're assigning the return value of the setHours() call to inlineSetup. This is not the Date object but a number representing the milliseconds of the date.
Why when a string of numbers of different length is passed to Date in Javascript sometimes returns a Date Object and sometimes Invalid Date.
For example :
new Date('123456') -> Tue Jan 01 123456 00:00:00 GMT+0530
new Date('1234567') -> Invalid Date
new Date('999999') -> Invalid Date
The way you are using the date constructor, the string is interpreted as the year. However, as Xotic750 already stated, dates in Javascript can only be in a range of -100,000,000 days to 100,000,000 days relative to 01 Jan, 1970 UTC. That means '123456' is in the range, but '1234567' and '999999' are not.
Note that using the Date constructor with a string is strongly discouraged because of inconsistency between browsers. It would be better to parse the date yourself and use the constructor taking years, months etc.
Why does a string of numbers work differently than actual numbers in a new Date():
var myfirstDate = new Date("2013, 10, 15"); //returns Tue Oct 15 2013 00:00:00 GMT-0500 (CDT)
var mysecondDate = new Date(2013, 9, 15); // also returns Tue Oct 15 2013 00:00:00 GMT-0500 (CDT)
myfirstDate.value == mysecondDate.value; //returns true
I looked at several tutorials and the idea of having a string like myfirstDate above isn't even mentioned. Does javascript automatically parse the string?
See the docs.
You're effectively invoking two different constructors.
The first one is parsed as a human-readable date:
new Date(dateString)
The second one expects 3 or more parameters, providing a year, a 0-based month number, and a day
new Date(year, month, day [, hour, minute, second, millisecond]);
year
Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
day
Integer value representing the day of the month (1-31).
Until ES5, parsing of date strings was entirely implementation dependent, though there were one or two strings that were consistently parsed by several browsers. ES5 introduced parsing of a version of ISO8601, however it's not supported by all browsers in use.
It is best to manually parse date string to ensure they are correct. There are various libraries to assist with that, but it isn't difficult (2 lines of code).
Incidentally, there is no Date.prototype.value method, so likely you are comparing undefined with itself. You should be comparing the time value, so:
myfirstDate.getTime() == mysecondDate.getTime();
or just:
myfirstDate == mysecondDate;
Oh, to answer the question: when the Date function is called as a constructor with a single string argument, it is treated as a date string and parsed (see above). So "10" represents October.
When Date is called as a constructor with more than one argument, they are treated as date values so 9 is treated as October since month arguments are zero indexed (0=January, 1=February, etc.).
I'm splitting an array, when I take the middle of the array which is reporting as epoch date in milliseconds. I'm testing the date using Epoch Converter and its valid.
I run the date() object and multiply by 1000 to adjust but I'm getting year 4000. I've switched to division just to test if I'm getting too large of a number the year is correct but the day and months are wrong....I've got to be missing something simple:
var jEtrim = item.DTM.split(/[(-]/);
var date = new Date(jEtrim[1] *1000);
sample output: Thu Jan 08 44037 07:03:20 GMT-0500 (Eastern Standard Time)
Here's the jEtrim: ["/Date", "1343151455000", "0400)/"]
Thanks in advance
Date takes its argument in milliseconds already, so you will not need to multiply by 1000. You will only need to convert it to a number:
new Date(+"1343151455000")