Difference between "new Date().valueOf()" and Date().valueOf()" - javascript

Why are the two expressions below returning different results?
Date().valueOf()
"Fri Feb 07 2014 16:03:34 GMT-0500 (Eastern Standard Time)"
new Date().valueOf()
1391807020802
Date().toString()
"Fri Feb 07 2014 16:09:21 GMT-0500 (Eastern Standard Time)"
new Date().toString()
"Fri Feb 07 2014 16:09:26 GMT-0500 (Eastern Standard Time)"

Date() returns a timestamp formatted as a string.
new Date() returns a Date instance.
Instances of the Date constructor have values that convert to numbers, which is why new Date().valueOf() returns a number. Strings are simply strings, so when you call Date().valueOf() you get the same string result.

Related

split string into json object

Is anyone can help for splitting the strings into JSON?
Below is few examples:
"name":"#MATRIX COPIL 33 agenda Sun Nov 20 2022 18:00:00 GMT-0500 (Eastern Standard Time)"
"name":"#CQI2022 COPIL 8 Tue Nov 22 2022 00:00:00 GMT+0100 (Central European Standard Time)"
"name":"#GED2023 Copil 10 Mon Nov 21 2022 18:00:00 GMT-0500 (Eastern Standard Time)"
Into Json objects like below
"project_name": "#GED"
"purpose": "COPIL"
"copil_number": "10"
"dateofcreation": "Sun Nov 20 2022 18:00:00 GMT-0500"
Also if it's possible to get the week number for Json object based on dateofcreation ?
"week_file": "47"

JavaScript converting long date to short date

I have the (german) Output: Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)
and I would like to have just the yyyy-mm-dd date: 2022-09-09
Thank you for the help!
Date's are not too great in JS, the reason that libs like moment.js etc are very popular.
But one idea is to simply replace the GMT+0300 to GMT+0000, and then parse into Date object and use ToISOString and slice the first 10 chars.
eg.
function shortDate(dt) {
return new Date(dt.replace(/GMT\+..../,'GMT+0000')).toISOString().slice(0, 10);
}
console.log(shortDate('Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)'));

Date Filter incorrectly formatting the date value

I have a variable dateSubmitted having the value dateSubmitted": "07-09-20:11:03:30
I am using Angular Version 7
I am using the date filter within HTML to format the date as below:
<td> {{element.dateSubmitted | date:'MMMM d, y'}} </td>
The date value is correct but the year is incorrectly showing as 2001 in the output July 9, 2001
The correct output is July 9, 2020
See DatePipe input value
value – The date expression:
a Date object
a number (milliseconds since UTC epoch)
an ISO string (https://www.w3.org/TR/NOTE-datetime).
Your date does not conform to ISO string specification, in particular, it has year at the end. The parser seems to interpret is as an hour.
Compare:
new Date('07-09:11:03:30')
// Sun Jul 01 2001 09:11:03 GMT+0200 (Central European Summer Time)
new Date('07-09-:11:03:30')
// Mon Jul 09 2001 11:03:30 GMT+0200 (Central European Summer Time)
new Date('07-09-20:11:03:30')
// Mon Jul 09 2001 20:11:03 GMT+0200 (Central European Summer Time)

javascript full text Date() format with PHP carbon

I am generating and sending full date string from javascript Date() function which returns full string date format like this:
Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)
Carbon parser wont accept this format for creating the same date on server side. This does not work:
$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)');
Error Failed to parse time string (Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)) at position 41 (l): Double timezone specification
If I remove (Central European Standard Time) works:
$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100');
Then it correctly creates date.
Can JS default Date() be used in Carbon somehow or will I have to format date before sending it to Carbon?
Carbon extends PHP's native DateTime class, so you can use createFromFormat instead:
$date = 'Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)';
$carbon = Carbon::createFromFormat('D M d Y H:i:s e+', $date);
The important part of the format specification is the + at the end, which tells it to ignore any trailing data.
See https://3v4l.org/Rnen7 for a demo (using DateTime rather than Carbon)
You can pass the date in ISO format, Carbon understands ISO format. You can get the date in ISO format using new Date().toISOString()

JavaScript: new Date(Date.parse("A 49"))

new Date(Date.parse("A49"))
// Invalid Date
new Date(Date.parse("A 49"))
// Fri Jan 01 2049 00:00:00 GMT+0600 (Ekaterinburg Standard Time)
new Date(Date.parse("Aasdfashdkfjahslkdf 49"))
// Fri Jan 01 2049 00:00:00 GMT+0600 (Ekaterinburg Standard Time)
Why "A 49" is valid DateTime?
The parser ignored your A (because your A can be something like a separator or just some user text that has no relation to the date), and just picked the number and took it as the year. But if you add a letter to the number the parser cant recognize it as a number, and consequently can not convert it to an date.
Examples:
new Date(Date.parse("A 49"))
//Fri Jan 01 2049 00:00:00 GMT+0100 (W. Europe Standard Time)
new Date(Date.parse("OTHER 49"))
//Fri Jan 01 2049 00:00:00 GMT+0100 (W. Europe Standard Time)
new Date(Date.parse("OTHER 10"))
//Mon Oct 01 2001 00:00:00 GMT+0200 (W. Europe Daylight Time)
new Date(Date.parse("OTHER 10a"))
//Invalid Date
new Date(Date.parse("OTHER 10 10"))
//Wed Oct 10 2001 00:00:00 GMT+0200 (W. Europe Daylight Time)
More about Date.parse("...")
Because it's parsing A 49 and extracting only 49 and assuming it's start of 2049.

Categories