I want to generate this date format using Moment.js:
Mon Apr 1 17:51:40 2019
Right now, I am getting this format instead:
Mon Apr 01 2019 17:51:40 GMT+0530 (IST)
use this:
Moment(new Date(this.state.date)).format('MMMM Do YYYY, h:mm:ss a')
or whatever format you want. you can check them on here: https://momentjs.com/
Try this:
var dateTime = new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)");
dateTime = moment(dateTime).format("ddd MMM D HH:mm:ss YYYY");
It's already a valid date string - just make a new Date:
console.log(new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)"));
first you need to install moment
let formatedDate = moment('Mon Apr 01 2019 17:51:40 GMT+0530 (IST)').format('ddd MMM DD HH:mm:ss YYYY');
console.log(formatedDate)
output: Mon Apr 01 17:51:40 2019
Done You can check it
Related
How do I go about comparing new Date().toUTCString() to a say Wed, 03 Oct 2018 01:33:29 GMT in moment?
I've tried moment(Wed, 03 Oct 2018 01:33:29 GMT).isBefore(moment(new Date().toUTCString())) but it doesn't seem to work.
The way you have explained, the comparison is working for me. Check the snippet. FYI, you need to use '' while creating a moment object from the string.
$(() => {
var a = moment('Wed, 03 Oct 2018 01:33:29 GMT');
var b = moment(new Date().toUTCString());
console.log(a.isBefore(b));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can simply do it as follows:
moment('Thu Oct 04 2018 07:35:54 GMT+0530 (IST)').isBefore(moment())
moment('Wed, 03 Oct 2018 01:33:29 GMT').isBefore(moment())
This should return you value. (true/false)
Dates string array is constructed from backend data object as below:
const dates: string[] = this.data.map((item:any) => item.date.jsdate);
The result is
dates = [
Thu Jan 12 2015 00:00:00 GMT+0530 (India Time),
Tue Feb 25 2015 00:00:00 GMT+0530 (India Time),
Sun Jan 28 2016 00:00:00 GMT+0530 (India Time)
]
typeof(dates) is "object"
How to find the get the latest date among the jsdates array?
Is it possible to apply max function of date-fns?
I can offer one solution, using the momentjs (https://momentjs.com/) library.
You can sort the dates using:
dates = dates.sort((a,b) => moment(a).diff(moment(b));
Then if you want the latest date you could take the last element from that array:
return dates[dates.length-1];
You should also be able to do this without moment:
dates = dates.sort((a,b) => new Date(b) - new Date(a));
Convert each to a Date object and compare using getTime() function.
var dates = [
'Thu Jan 12 2015 00:00:00 GMT+0530 (India Time)',
'Tue Feb 25 2015 00:00:00 GMT+0530 (India Time)',
'Sun Jan 28 2016 00:00:00 GMT+0530 (India Time)'
]
console.log(
dates.sort((a,b)=>new Date(b).getTime() - new Date(a).getTime())[0]
)
var date1 = new Date('1900-01-01');
console.log(date1);
Yields:
"Mon Jan 01 1900 01:00:00 GMT+0100 (W. Europe Standard Time)"
var date2 = new Date(1900,1,1);
console.log(date2);
Yields:
"Thu Feb 01 1900 00:00:00 GMT+0100 (W. Europe Standard Time)"
Fiddle
But I don't understand why!
You can see the month difference since when you pass individual components (year, month, day, etc) to the Date object constructor, you have to consider that month parameter should start with 0:
console.log( new Date('1900-01-01').getMonth() ); // 0
Other than Jan/Feb there shouldn't be any differences in dates.
MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
What is the correct way to format this when using MomentJS's fromNow() functionality?
The date was generated using Javascripts Date.now() function.
Thu Nov 19 2015 19:58:03 GMT+0000 (GMT)
My first attempt was this.
var date = Thu Nov 19 2015 19:58:03 GMT+0000 (GMT);
var parsed = moment(date, "dd MMM d YYYY h:mm:ss").fromNow();
But I'm quite sure that dd MMM d YYYY h:mm:ss isn't the correct way to format this.
You can simply use:
var date = "Thu Nov 19 2015 19:58:03 GMT+0000 (GMT)";
moment(date).fromNow(); // 1 hour ago
See: http://momentjs.com/docs/#/parsing/string/
I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?
To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.