Cannot convert string to date - Invalid Date - javascript

I've this date: 16-08-13 I want convert it into DateTime 'cause I need to insert this value inside my database.
Actually I did this:
date = new Date('16-08-13');
but I get this error:
Invalid Date

From the Date documentation for the Date constructor taking a date string
dateString
String value representing a date. The string should be in a format
recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).
From the spec, a 2-digit year won't cut it. You want YYYY-MM-DD at the minimum:
console.log(new Date('2016-08-13'))

Simply add the time element to the input string.
var date = new Date('2016-08-13 00:00');
Your date format is confusing because 13-08-16 could mean the 13th day of the 8th month of the 2016 or the 16th day of the 8th month of 2013

You just have to make the year 4 digits
date = new Date('2016-08-13')
that will give you this results
Date {Fri Aug 12 2016 19:00:00 GMT-0500 (Central Standard Time)}
which is a datetime object.

Related

Why converting new.Date() .toISOString() changes the time?

I'm inserting a date in a database in two different format.
this is inserting as Datetime
var mydate;
mydate = new Date();
document.getElementById('clockinhour').value = mydate.toISOString().slice(0, 19).replace('T', ' ');
Output A
2017-06-21 20:14:31
this is inserting as varchar :
document.getElementById('clocked_in_time').value = Date();
Output B
Wed Jun 21 2017 16:14:31 GMT-0400 (Eastern Standard Time)
Output B is the correct time but I need to display output A. What causes the time to change when converted toISOString? How can I fix this?
In your this is inserting as Datetime block your slice are stripping of the timezone part (the Z at the end of toISOString output):
document.getElementById('clockinhour').value = mydate.toISOString().slice(0, 19).replace('T', ' ');
As pointed out by #RobG in the comments section, toISOString should always return the date in UTC (Z or +00:00).
RTFM:
"The time zone [offset] is always UTC, denoted by the suffix Z",
The time "changes" because it is converted to UTC when you calls toISOString.
If you want to get ISO date in your timezone, you should take a look in these two questions: How to ISO 8601 format a Date with Timezone Offset in JavaScript? and How to format a JavaScript date
ISO time is time zone free. You'll notice with b you have time zone GMT-04:00 if you add those four hours to the 16 hours in the Date, you get 20

Understanding Date in javascript when a string of numbers is passed to the Date object

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.

How to convert this date 'Wed Mar 9 09:48:09 PST 2016' to 'YYYY-MM-DD HH:mm:ss' format?

I am trying to convert datetime value from this format Wed Mar 9 09:48:09 PST 2016 into the following format YYYY-MM-DD HH:mm:ss
I tried to use moment but it is giving me a warning.
"Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
fa/<#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:9493
ia#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:10363
Ca#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15185
Ba#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15024
Aa#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:14677
Da#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15569
Ea#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15610
a#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:41
#http://localhost:1820/Home/Test:89:29
jQuery.event.dispatch#http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:5225:16
jQuery.event.add/elemData.handle#http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:4878:6
"
according to https://github.com/moment/moment/issues/1407 I should not be trying to use moment() to do this since it is not reliable.
How can I reliably convert the Wed Mar 9 09:48:09 PST 2016 into the following format YYYY-MM-DD HH:mm:ss?
You could try using Date.toJSON() , String.prototype.replace() , trim()
var date = new Date("Wed Mar 9 09:48:09 PST 2016").toJSON()
.replace(/(T)|(\..+$)/g, function(match, p1, p2) {
return match === p1 ? " " : ""
});
console.log(date);
Since you tagged your question with moment, I'll answer using moment.
First, the deprecation is because you are parsing a date string without supplying a format specification, and the string is not one of the standard ISO 8601 formats that moment can recognize directly. Use a format specifier and it will work just fine.
var m = moment("Wed Mar 9 09:48:09 PST 2016","ddd MMM D HH:mm:ss zz YYYY");
var s = m.format("YYYY-MM-DD HH:mm:ss"); // "2016-03-09 09:48:09"
Secondly, recognize that in the above code, zz is just a placeholder. Moment does not actually interpret time zone abbreviations because there are just too many ambiguities ("CST" has 5 different meanings). If you needed to interpret this as -08:00, then you'd have to do some string replacements on your own.
Fortunately, it would appear (at least from what you asked) that you don't want any time zone conversions at all, and thus the above code will do the job.

Javascript date object automatically add one day when creating from date string

In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?
It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.

Date object using string

I have a string date coming from a server and wanted to see if there was a way to get a JS date object from it.
Example: format mm/yy
exDate = "0919"
I want to return a new date object with the last day of the month also added to it. So for example the above would return
Mon Sep 30 2019 00:00:00 GMT-0500 (EST)
Is the above possible with just a 2 digit year and returning the last day in the month with the date object?
Parse in the string to a date object specifying the month ahead and 0 for the date:
var t = new Date("20" + exDate.substring(2, 4), exDate.substring(0, 2), 0);

Categories

Resources