Issue parsing date format M/D/YYYY - javascript

I have a string date in the format M/D/YYYY and I'm trying to format it into a new Date() date. I've tried the following and it's printing out Invalid Date.
console.log(date)
console.log(moment(date, "MM/DD/YYYY").toDate())
console.log(moment(date, "M/D/YYYY").toDate())
3/10/2017
2017-10-03T04:00:00.000Z
2017-10-03T04:00:00.000Z
3/13/2017
Invalid Date
Invalid Date
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js
This parses correctly without anything but throws a massive warning.
var date = '2/23/2017';
console.log(moment(date, "MM/DD/YYYY").toDate())
console.log(moment(date, "M/D/YYYY").toDate())
var date = '3 / 13 / 2017';
console.log(moment(date, "MM/DD/YYYY").toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
console.log(moment(date).toDate())

You can try like this moment(date, "MM/DD/YYYY").toDate();

Try using Date.parse like the following:
new Date(Date.parse(date));
new Date(Date.parse("2017-10-03T04:00:00.000Z"));
// > Tue Oct 03 2017 00:00:00 GMT-0400 (EDT)
You can then use the built-in browsers functions .getDate(), .getMonth(), .getFullYear() /etc. to get the attributes of the above
var myDate = new Date(Date.parse(date));
console.log(myDate.getMonth())

This works!
moment(date, "l").toDate()

Related

How in javascript to convert a date to a specific format

I would like to know how can I convert with a library or pure JS a date like this
Fri Mar 05 2021 13:51:35 GMT+0000 (Coordinated Universal Time)
to this format
2021-03-05 13:51:35.829058+00
As asked in comments what I was trying to achieve is to convert the shared date to specific format I tried the following stuff
1) const t = createdAt
.toISOString()
.replace('T', ' ')
.replace('Z', '');
2) Using date-fns
format(addMinutes(date, date.getTimezoneOffset()), 'yyyy-MM-dd HH:mm:ss');
The result of those tries is like
2021-03-05 14:44:11
But is not correct as I need after 11 more numbers and +00.
When you use UTCDate(), you need have a full date to get all items:
function pad(e){ return (e.toString().length<2?'0'+e:e); }
function ShowData(data){ var d = new Date(data); return d.getFullYear()+'-'+pad(d.getMonth()+1) +'-'+ pad(d.getDate())+' '+pad(d.getHours())+':'+pad(d.getMinutes())+':'+pad(d.getSeconds())+'.'+pad(d.getMilliseconds())+pad(d.getUTCDate())+"+00"; }
When you use your example:
ShowData('Fri Mar 05 2021 13:51:35 GMT+0000');
Then you got
2021-03-05 10:51:35.0005+00
When we use a full date form:
ShowData(new Date());
Then you got
2021-03-05 13:05:36.51605+00
I hope could help.

Converting a UTC date string to another timezone maintaining the same format

I'd like to convert a UTC date string into the current users timezone and maintain the format of the date string.
For example, I have this code which works:
var data = '2017-04-24 12:06:37';
var date = new Date(data+' UTC');
console.log(date.toString()); // logs "Mon Apr 24 2017 08:06:37 GMT-0400 (Eastern Daylight Time)"
How do I get this to output the new date string in the exact same format dynamically?
You have to add UTC to the string before you convert it to a date.
To convert any date to a UTC string, you can use
var UTCstring = (new Date()).toUTCString();
See my snippet. It compares the five different methods.
var date = new Date('2017-04-24 12:06:37 PM UTC');
var local = date.toString();
var utc = date.toUTCString();
var iso = date.toISOString();
var dateString = date.toDateString();
console.log(date);
console.log(local);
console.log(utc);
console.log(iso);
console.log(dateString);
Solution I used with momentjs:
moment.utc(value).local().format('YYYY-MM-DD HH:mm:ss');

How to convert string date to date javascript?

My code is like this :
createDate = '2016-07-12 09:09:38';
createDate = new Date(createDate);
console.log(createDate);
The result : Date {Invalid Date}
I want the result like this : Date {Tue Jul 12 2016 09:53:13 GMT+0700 (SE Asia Standard Time)}
Any solution to solve my problem?
Use this ISO-8601 format:
createDate = '2016-07-12T09:09:38'; //watch for the T in between
createDate = new Date(createDate);
console.log(createDate);
here UTC time zone used to interpret arguments.
Check This

Remove time from GMT time format

I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.
How can I strip out everything except for the actual date?
If you want to keep using Date and not String you could do this:
var d=new Date(); //your date object
console.log(new Date(d.setHours(0,0,0,0)));
-PS, you don't need a new Date object, it's just an example in case you want to log it to the console.
http://www.w3schools.com/jsref/jsref_sethours.asp
Like this:
var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
dateString = new Date(dateString).toUTCString();
dateString = dateString.split(' ').slice(0, 4).join(' ');
console.log(dateString);
I'm using this workaround :
// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())
You could use Moment.js, a library that provides many helper functions to validate, manipulate, display and format dates and times in JavaScript.
Using Moment.js lib:
var dateString = new Date('Mon Jan 12 00:00:00 GMT 2015');
moment(dateString).format('YYYY-MM-DD HH:mm');
Or simplified:
moment('Mon Jan 12 00:00:00 GMT 2015').format('YYYY-MM-DD HH:mm')
Well,
Here is my Solution
let dateString = 'Mon May 25 01:07:00 GMT 2020';
let dateObj = new Date(dateString);
console.log(dateObj.toDateString());
// outputs Mon May 25 2020
See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
Just cut it with substring:
var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);
In this case you can just manipulate your string without the use of a Date object.
var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
date = dateTime.split(' ', 4).join(' ');
document.body.appendChild(document.createTextNode(date));
You can first convert the date to String:
String dateString = String.valueOf(date);
Then apply substring to the String:
dateString.substring(4, 11) + dateString.substring(30);
You need to take care as converting date to String will actually change the date format as well.

How to convert string to date format in javascript?

How can convert a string to date format in java script.
I want to convert this string "Wed May 15 2013 11:30:00 GMT 0530 (IST)" into a date format.
Use Date.parse function
// create Date object from string
var d = new Date(Date.parse("Wed May 15 2013 11:30:00 GMT 0530 (IST)"));
var dateString = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
<script type="javascript">
var dateTime = new Date();
alert(dateTime.toString());
</script>
Here is the working Demo: Demo

Categories