MomentJS Date.now() format - javascript

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/

Related

How to convert string date-time range to javascript date objects?

I have a date-time range string that looks like this.
2019-06-14, 12:00:00 AM - 2019-06-15, 12:00:00 AM
2019-06-14, 12:00:00 AM is the start date.
2019-06-15, 12:00:00 AM is the end date.
I want to convert this string into two javascript date objects named "startdate" and "enddate".
How do i do that? I am comfortable using moment.js as well, so a solution with moment.js will also work for me.
Thanks!
Try this code below
function getStartAndEndDate( dateStr ){
let dates = dateStr.split( " - " );
let start = new Date( dates[0] );
let end = new Date( dates[1] );
return{ startDate: start, endDate: end };
}
Usage:
getStartAndEndDate( "2019-06-14, 12:00:00 AM - 2019-06-15, 12:00:00 AM" );
Output
{startDate: Fri Jun 14 2019 00:00:00 GMT-0500 (Central Daylight Time), endDate: Sat Jun 15 2019 00:00:00 GMT-0500 (Central Daylight Time)}
The best way to do this is using new Date()
Example:
var stringDate = '2019-06-14, 12:00:00 AM '
var startDate = new Date(stringDate)
console.log(startDate)
var stringDate2 = '2019-06-15, 12:00:00 AM'
var finishDate = new Date(stringDate2)
console.log(finishDate)

How to convert date into given format?

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

Easy way to convert a timestamp to a human readable date

I have the following timestamp: 2016-03-29T14:14:43.000Z. Is there any easy way to use JavaScript to make it look something like the following: Mar 29, 2016 2:14p? I tried using Date.parse() but it didn't seem to do anything.
{{yourValue| date:"MMM d, yyyy h:ma"}}
var ts = "2016-03-29T14:14:43.000Z";
var date = new Date(ts);
console.log(date); // Displays Tue Mar 29 2016 16:14:43 GMT+0200 (Romance Summer Time)
Is that what you need ?
var ds = date.toUTCString();
console.log(ds.substr(0,24)); // Displays Tue Mar 29 2016 16:14:43

Ignore UTC in javascript dates

I have got this:
startDate = new Date();
startDate = new Date(Date.parse('2014-10-20'));
finishDate = new Date();
finishDate = new Date(Date.parse('2014-11-21'));
The startDate is Mon Oct 20 2014 01:00:00 GMT+0100 (Hora de Verão de GMT);
The finishDate is Fri Nov 21 2014 00:00:00 GMT+0000 (Hora padrão de GMT);
You can see the GMT+0100 in the startDate and not in the finishDate, possibly because after 26 October the winter time is on.
I want to get those dates without regard to the timezone, winter or summer time.
You could use Date#toUTCString(). This will return a string with the date in UTC timezone, which naturally will ignore daylight savings time.

How to get timezone without moment-timezone.js

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.

Categories