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.
Related
I have a requirement to send the current system date to microservices on search. The time should include milliseconds information as well. For now I was sending new Date() for the same and it looked like:
Thu Aug 31 2017 15:06:37 GMT+0530 (India Standard Time)
However I need the milliseconds information as well so the time should look like:
Thu Aug 31 2017 15:06:37.228 GMT+0530 (India Standard Time)
Here 228 is the millisecond at that moment that I can extract using getMilliseconds() method of date. The question is how can I add this in the date so that it works for all locations wherever the application is accessed?
If you don't mind having the result as a string, this will show the output you are looking for:
// ES5
var fmtDateMsES5 = function(date) {
var splitDate = date.toString().split(' ');
splitDate[4] = splitDate[4] + '.' + date.getMilliseconds();
return splitDate.join(' ');
}
// log output (ES5)
console.log('ES5 output\n', fmtDateMsES5(new Date()));
// ES6
const fmtDateMsES6 = date => {
const splitDate = date.toString().split(' ');
splitDate[4] = `${splitDate[4]}.${date.getMilliseconds()}`;
return splitDate.join(' ');
};
// log output (ES6)
console.log('ES6 output\n', fmtDateMsES6(new Date()));
// ES5 and ES6 functions logged simultaneously
console.log(
`\nES5 and ES6 functions logged simultaneously`,
`\n${'-'.repeat(55)}`,
`\nES5 output ${fmtDateMsES5(new Date())}`,
`\nES6 output ${fmtDateMsES6(new Date())}`
);
Initially I saw the format method on the Date object but this is not built-in and requires a library.
If you must use a time library I would recommend the excellent moment.js and use the "SSS" syntax to get the milliseconds, for example:
var now = moment().format('MMM DD h:mm.SSS A');
//Sep 12 8:21.167 AM
http://jsfiddle.net/kLL2eobh/
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()
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 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
How to format date and time like this in JavaScript ?
March 05, 2012 # 14:30 (UTC - 9:30)
I use this code to calculate EST time :
function getDate() {
var now = new Date();
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * -4));
}
I use the date-time-format that Tats recommended because doing it manually is a huge PIA.
var yourDate = dateFormat(getDate(), "mmmm dd, yyyy # HH:MM) + "(UTC -9:30)";
Keep in mind this isn't Daylight Savings aware.. and you are asking for UTC -9:30 in your format, but your function converts to -4. Also, I believe that now.getTime returns in UTC.. so you can just add your difference there.
JavaScript Date Format
Check out date.js! It's a really powerful little library for working with Dates in JavaScript.
To get today's date in EST, you can do something like...
var today = new Date();
today.toString(); // outputs "Wed Apr 11 2012 15:40:40 GMT-0500 (CDT)"
today.setTimezone("EST");
today.toString(); // outputs "Wed Apr 11 2012 14:40:40 GMT-0500 (CDT)"
Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.
If you do just
var now = new Date();
document.write(now);
you will get
Wed Mar 14 2012 20:53:06 GMT+0000 (GMT Standard Time)
Link1, Link2.
Is it what you want?