Converting Twitter's created_at date using JavaScript? - javascript

I am using a combination of PHP/JavaScript to pull in Tweets from Twitter and trying to convert the created_at response to just show the month and day.
My JSON response for the created_at date data[0].created_at returns this:
Wed Oct 15 21:30:47 +0000 2014
And I would like to convert it to this:
Oct 15
Is it possible to do this in JavaScript? Thank you.

//split it into an array
//splice and only keep the two words that contain date information
//join it with a space
'Wed Oct 15 21:30:47 +0000 2014'.split(' ').splice(1,2).join(' ');
//in case you need the year too
//get the year by going to the last array index and join that too
var split = 'Wed Oct 15 21:30:47 +0000 2014'.split(' ');
var date = split.splice(1,2);
var year = split[split.length-1];
date.push(year);
date = date.join(' ');

Related

To change time format when sending from server(node) to client (react)

I'm struggling with dates format.
On server side which is done in Node.js I have an array of dates formatted like this:
["2018-05-31T22:00:00.000Z", "2018-06-14T22:00:00.000Z"]
What I need on the client side (React.js) is array of time formatted like that:
[Fri Jun 01 2018 00:00:00 GMT+0200 (CEST), Sat Jun 02 2018 00:00:00 GMT+0200 (CEST)]
So not only time formats are different and needs to be changed but first ones are in strings, other are not (actually, what are they? ;) )
I guess the fact that both are in array is of not much importance here I guess.
Thank you for helping
You can iterate the array of stringified Dates, once you have it on the client side, and convert back each String item to a Date Object.
let dateString = ["2018-05-31T22:00:00.000Z", "2018-06-14T22:00:00.000Z"];
let dateObj = dateString.map( d => new Date(d) );
// done. dates Array contains now Date Objects
console.log( typeof dateObj[0] );

how to convert milliseconds to gmt time using javascript

I have a project where if the end date is set for February 12 (can be set to any future date), the following is obtained from the API response.
project: {endDateTime:"1518393600000"}
For UTC time and date, this response corresponds to Mon Feb 12 2018 00:00:00
For local time and date, the response corresponds to Sun Feb 11 2018 19:00:00 (GMT - 05:00)
On the UI, I need to show the end date as Feb 12, 2018, but the date is getting converted to the local date and time zone and shows Feb 11 as the end date. My code is below:
var d = new Date();
var c = d.setTime(parseInt($scope.project.endDateTime));
$scope.endDateTime = c;
In the html
<div> {{endDateTime}} </div>
I tried modifying the code in the following way but it did not work.
var d = new Date($scope.project.endDateTime);
var c = d.getUTCDate();
$scope.endDateTime = c;
I tried to tune the code in other ways but could not get it to work. I know similar questions have been asked before but still could not get it to work, even after spending several hours. Maybe I am missing something very trivial. Some help would be greatly appreciated. :)
I have figured out the solution. My code and explanation is as follows
var d = new Date(parseInt($scope.project.endDateTime));
var c = c.toUTCString();
var endDate= c.split(" ");
$scope.endDateTime = endDate[2] + ' ' + endDate[1] + ',' + ' ' + endDate[3];
And the html is
<div> {{endDateTime}}</div>
I needed to parse the string that came back from the API response. Missed this part and also the correct method is toUTCString(), not getUTCDate().
So basically what happens is if the end date is set to Feb 12(in this case, it is dynamic though), the API returns
project: {endDateTime:"1518393600000"}
The first line of the code parses the string and if I do a console.log(d) I get this: Sun Feb 11 2018 19:00:00 GMT-0500 (Eastern Standard Time).
Now I need to display the time in GMT so the toUTCString() method was used in the second line and doing console.log(c) I get: Mon, 12 Feb 2018 00:00:00 GMT
The requirement is to show the date as Feb 12, 2018 on the UI, so I split the string in the third line and if i do console.log(endDate) I get
["Mon,", "12", "Feb", "2018", "00:00:00", "GMT"]
From the output of the third line, it was pretty easy to show the required date format. Hope this helps.

How to extract only date from moment object

I am using moment object for date.
and for that i wrote
dateOnly = moment(date.format("MM/DD/YYYY"));
var ndate = dateOnly.toDate();
I want to display only date on front end but it is showing me in this format that I don't want
Mon Feb 22 2016 00:00:00 GMT+0530 (India Standard Time)
You can use moment's format function to display date in the format you want to
moment().format('MM/DD/YYYY'); // "02/22/2016"
You can go through all the available formats here
http://momentjs.com/docs/#/displaying/
var dateOnly = moment(date).format('MM/DD/YYYY'); // for formatted dates.
yesterday= moment(date).subtract(1,'days') // for previous day
//you can replace days with months and years.
tomorrow= moment(date).add(1,'days') // for upcoming day
//you can replace days with months and years.
var date1= moment(yesterday)
var date2= moment(tomorrow)
differencebetweenmoments =moment(date1).diff(moment(date2),'days');

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);

append string with matched substring

Im having issues with formatting a string received from the Twitter API. Im using timeago plugin and returns NaN in IE 10. After hunting it down it seems to be because the time recieved is formatted like so:
Tue Apr 02 14:27:31 +0000 2013
instead of:
Tue Apr 02 14:27:31 2013 +0000
Anyone know a quick way of matching if this is present and then appending the string if it is?
regards
You can use splice and split method to achive this.
var time="Tue Apr 02 14:27:31 +0000 2013";
var arr = time.split(" ");
var year = arr.splice(arr.length-1,1);
arr.splice(arr.length-1,0, year[0]);
//console.log(arr.join(" "));
You can do this:
Var dateStr; // your date string
Var index = dateStr.indexOf('+');
If (index === dateStr.length-5) {
// you're in format two, what you wanted
}
The caveat is that this date has to come in the way you displayed it.
Using regular expressions you could use this:
var originalDate = "Tue Apr 02 14:27:31 +0000 2013";
// find the last two number blocks and swap them
var reformattedDate = originalDate.replace(/ (\+[0-9]+) ([0-9]{4})$/, "$2 $1");
This will switch the position of the last two number-blocks in the original date.
Also this method will not change dates that don't match the regular expression. If you get an originalDate that already has the format "2013 +0000" it will not be changed. ;)

Categories

Resources