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. ;)
Related
i have no idea. I dont know how to read it correctly, so it would be nice if someone would help as im pretty new to Javascript. it would be usefull if the numbers would be in a one variable, thank you!
You can use a regular expression to remove all characters that are not a number.
var s = "Mon, 05 Feb 2018 21:00:39 +0000";
var nums = s.replace(/\D/g, "");
console.log(nums);
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.
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(' ');
{"date":"Thu Dec 06 14:56:01 IST 2012"}
I am getting this string as JSON can I convert it to JS date object?
Edit: Unfortunately i was totally wrong, sry for that,my bad, it happened to always result in today,
but to not screw you up, heres an solution which should work for you anyway
If you get Different Time strings from your Server, maybe the best way is to write a Regex pattern that matches your String patterns
Access your date propertie from your JSON Object
Since instantiating a Date object with this "Thu Dec 06 14:56:01 IST 2012" String would result in an Invalid Date
Remove the "IST" myJson.date.replace(" IST","")
Instantiate the your Date object with your new String myDate = new Date("Thu Dec 06 14:56:01 2012")
Now theres really your Date Object
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2012"}
var myDate = new Date(myJson.date.replace(" IST",""))
console.log(myDate.toLocaleDateString())
Heres the JSBin
The right way to convert your JSON to the data object it's parsing this date as a string.
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2013"}
var myDate = new Date(Date(myJson.date))
console.log(myDate.getFullYear()) // 2012
Doesn't work with a Year different from the current one.
Related link
Where can I find documentation on formatting a date in JavaScript?
I'm trying to truncate a JavaScript Date object string from:
Wed Aug 01 2012 06:00:00 GMT-0700 (Pacific Daylight Time)
to
Wed Aug 01 2012
(I'm not particular, could be of format MM/DD/YYYY for example, as long as I get rid of the time/timezone)
Essentially I just want to get rid of the time and the timezone because I need to do a === comparison with another date (that doesn't include the time or timezone)
I've tried using this http://www.mattkruse.com/javascript/date/index.html but it was to no avail. Does anyone know how I can format the existing string such as to get rid of the time? I would prefer to stay away from substring functions, but if that's the only option then I guess I'll have to settle.
Edit: I'm open to options that will compare two date objects/strings and return true if the date is the same and the time is different.
The only way to get a specific format of date across different browsers is to create it yourself. The Date methods in ECMAScript are all implementation dependent.
If you have a date object, then:
// For format Wed Aug 01 2012
function formatDate(obj) {
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
return days[obj.getDay()] + ' ' + months[obj.getMonth()] +
' ' + obj.getDate() + ' ' + obj.getFullYear();
}
Though a more widely used format is Wed 01 Aug 2012
Use the Date object's toDateString() method instead of its toString() method.
SIDE BY SIDE DEMO
Even so, it might be better to compare the two date objects directly:
Compare two dates with JavaScript