Parse Credit Card input from Magnetic Stripe - asp.net
Does anyone know how to parse a credit card string input from a Magnetic Card Swiper?
I tried a JavaScript parser but never got it to work. This is what the input looks like.
%BNNNNNNNNNNNNNNNN^DOE/JOHN
^1210201901000101000100061000000?;NNNNNNNNNNNNNNNN=12102019010106111001?
The N's are the credit card number.
See the Magnetic Stripe Card entry # Wikipedia:
Track one, Format B:
Start sentinel — one character (generally '%')
Format code="B" — one character (alpha only)
Primary account number (PAN) — up to 19 characters. Usually, but not
always, matches the credit card number
printed on the front of the card.
Field Separator — one character (generally '^')
Name — two to 26 characters
Field Separator — one character (generally '^')
Expiration date — four characters in the form YYMM.
Service code — three characters
Discretionary data — may include Pin Verification Key Indicator (PVKI,
1 character), PIN Verification Value
(PVV, 4 characters), Card Verification
Value or Card Verification Code (CVV
or CVK, 3 characters)
End sentinel — one character (generally '?')
Longitudinal redundancy check (LRC) — one character (Most reader devices
do not return this value when the card
is swiped to the presentation layer,
and use it only to verify the input
internally to the reader.)
I hope the data is fake, otherwise Anyone could get the:
Name
Expiration Date
CVV
And I'm not sure but I think the credit card number (or # of possibilities) can be computed using the LRC.
I did you one better: I made a video showing how to do exactly this with ASP.Net/c#:
http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing
Here is the section of code that you probably care about:
protected void CardReader_OTC(object sender, EventArgs e)
{
bool CaretPresent = false;
bool EqualPresent = false;
CaretPresent = CardReader.Text.Contains("^");
EqualPresent = CardReader.Text.Contains("=");
if (CaretPresent)
{
string[] CardData = CardReader.Text.Split('^');
//B1234123412341234^CardUser/John^030510100000019301000000877000000?
PersonName.Text = FormatName(CardData[1]);
CardNumber.Text = FormatCardNumber(CardData[0]);
CardExpiration.Text = CardData[2].Substring(2, 2) + "/" + CardData[2].Substring(0, 2);
}
else if (EqualPresent)
{
string[] CardData = CardReader.Text.Split('=');
//1234123412341234=0305101193010877?
CardNumber.Text = FormatCardNumber(CardData[0]);
CardExpiration.Text = CardData[1].Substring(2, 2) + "/" + CardData[1].Substring(0, 2);
}
}
The complete code is on that website I linked above.
From what I can remember:
That is a two-track magnetic strip data - first track starts with % and ends with ?, the second track starts with ; and ends with ?. These are Start/End markers.
The first track is alphanumeric, the second track is numeric, and there is a third track which is numeric also (if my memory serves correct).
The data between the start/end markers can be variable depending on the recording density of the magnetic strip. The higher the density, the more it can be recorded on one track.
Using a regex to get at the data may not be a reliable method to pick out the information required.
And not all credit cards have exactly two tracks, some uses three tracks.
Generally for a card-not present transaction (i.e. MOTO transactions) you will need cc#, expiry and possibly the CVV (aka CVC2 etc). You can obtain the first 2 from a card-swipe as this in the track data. CVV is printed on the card.
Name on card doesn't matter so much. Unless your acquirer and the cardholder are using address verification, but you can find that between ^^, it may have white space padding which you can remove.
The part you want is track2 NNNNNNNNNNNNNNNN=1210 where NNNNN=card number PAN, and 1210 = Expiry date.
Even if track1 is empty (which sometimes it is as it's not used in processing), you will still get the ;?, so you could use the index of the second ; as start of the string and = as the end of the cc# string. With the 4 characters after the = as the expiry.
I would advise getting the card holder to sign something in record of the transaction otherwise they could dispute the card and do a charge-back.
And not all credit cards have exactly two tracks, some uses three tracks.
Only track2 is used for processing and has a standardized format.
Debit cards can't generally be processed (unless they have a visa-debit card or something).
P.S. you shouldn't store cc data in plain text, so try and keep everything in mem or strong encryption.
Try this :
https://github.com/pdamer/CardReader/blob/master/CardReader.js
Or this:
http://blog.cnizz.com/2008/10/16/javascript-snippet-for-handling-credit-card-readers/
I think that what u need
here is my code:
1st the listener to get the data.... this data needs validation which i am looking for help on. A good swipe works fine, but a bad swipe will cause an error in the parser.
$('#cc-dialog-form').keypress(function(e)
{
var charCode = e.which;
//ie? evt = e || window.event;
track_start = '%';
finished = false;
timeout = 100;
track_start_code = track_start.charCodeAt(0);
//console.log('Track_start_code: ' + track_start_code);
//console.log('keycode ' + e.keycode);
//console.log('charcode ' + charCode);
//console.log('track_start_code ' + track_start_code);
if (charCode == track_start_code)
{
collect_track_data = true;
$('#offline_cc_entry').hide();
$('#cc_online').hide();
$('#Manual_CC_DATA').hide();
$('#cc_loading_image').show();
}
if (collect_track_data)
{
if (charCode == $.ui.keyCode.ENTER)
{
//all done
//console.log( card_data);
collect_track_data = false;
$('#cc_loading_image').hide();
$('#Manual_CC_DATA').show();
//console.log("Track Data: " + card_data);
process_swipe_cc_payment(card_data);
card_data = '';
}
else
{
card_data = card_data + String.fromCharCode(charCode);
console.log(card_data);
if (e.preventDefault) e.preventDefault();
e.returnValue=false;
return false;
}
}
else
{
//i am guessing this will be regular input?
if (charCode == $.ui.keyCode.ENTER)
{
process_keyed_or_offline_CC_payment();
}
}
//console.log("which: " + e.which);
//console.log("keyCode: " + e.keyCode);
//track and collect data here?
});
And here is the parser.... note I put it all in one function so I can destroy all the variables so they are not lingering in a browser.
parse_data = true;
if (parse_data)
{
var parsed_card_data = {};
parsed_card_data['card_data'] = card_data;
var tracks = card_data.split("?");
//console.log ("tracks");
//console.log (tracks);
parsed_card_data['track1'] = tracks[0];
parsed_card_data['track2'] = tracks[1];
//if there is a third track we might find it under tracks[2]
//splitting the card data OPTION 1
var track1_parsed = tracks[0].split("^");
//console.log (track1_parsed);
//track1 data....
var card_number_track1 = track1_parsed[0].substring(2);
parsed_card_data['card_number_track1'] = card_number_track1;
var details2_1 = tracks[1].split(";");
details2_1 = details2_1[1].split("=");
var exp_date_track_1 = details2_1[1];
exp_date_track_1 = exp_date_track_1.substring(0, exp_date_track_1.length - 1);
exp_date_track_1 = exp_date_track_1.substring(2, 4) + "/" + exp_date_track_1.substring(0,2);
parsed_card_data['exp_track1'] = exp_date_track_1;
//now check if track one matches track 2...
track2_parsed = tracks[1].split("=");
card_number_track_2 = track2_parsed[0].substring(1);
parsed_card_data['card_number_track_2'] = card_number_track_2;
exp_date_track_2 = track2_parsed[1].substring(0,4);
exp_date_track_2 = exp_date_track_2.substring(2, 4) + "/" + exp_date_track_2.substring(0,2);
parsed_card_data['exp_date_track_2'] = exp_date_track_2;
var primary_account_number = card_number_track1.substring(0,1);
if(card_number_track1 == card_number_track_2 && exp_date_track_1 == exp_date_track_2)
{
//now make a security feature showing the last 4 digits only....
parsed_card_data['secure_card_number'] = "xxxx " + card_number_track1.substring(card_number_track1.length-4, card_number_track1.length);
if(card_number_track1.length == 15)
{
parsed_card_data['card_type'] = "American Express";
}
else if(primary_account_number == 4)
{
parsed_card_data['card_type'] = "Visa";
}
else if(primary_account_number == 5)
{
parsed_card_data['card_type'] = "Master Card";
}
else if(primary_account_number == 6)
{
parsed_card_data['card_type'] = "Discover";
}
else
{
parsed_card_data['card_type'] = false;
}
var names_1 = track1_parsed[1].split("/");
parsed_card_data['first_name'] = names_1[1].trim();
parsed_card_data['last_name'] = names_1[0].trim();
//console.log("return Data");
//console.log(return_data);
}
else
{
parsed_card_data = false;
}
//zero out the variables...
tracks = '';
track1_parsed = '';
card_number_track1 = '';
details2_1 = '';
exp_date_track_1 = '';
track2_parsed = '';
card_number_track_2 = '';
exp_date_track_2 = '';
primary_account_number = '';
}
if(parsed_card_data)
{
//console.log(parsed_card_data);
$('#card_type').val(parsed_card_data['card_type']);
$('#credit_card_number').val(parsed_card_data['secure_card_number']);
$('#expiration').val(parsed_card_data['exp']);
$('#card_holder').val(parsed_card_data['first_name']+ " " + parsed_card_data['last_name']);
//parsed_card_data['track1'] is basically what we want???
$('#CC_SWIPE_INSTRUCTIONS').hide();
$('#CC_DATA').hide();
$('#cc_loading_image').show();
var post_string = {};
post_string['ajax_request'] = 'CREDIT_CARD_PAYMENT';
post_string['amount'] = $('#cc_input').val();
post_string['card_data'] = parsed_card_data;
post_string['pos_sales_invoice_id'] = pos_sales_invoice_id;
post_string['pos_payment_gateway_id'] = $('#pos_payment_gateway_id').val();
post_string['line'] = 'online';
post_string['swipe'] = 'swipe';
card_data = '';
parsed_card_data = {};
var url = 'ajax_requests.php';
$.ajax({
type: 'POST',
url: url,
data: post_string,
async: true,
success: function(response)
{
$('#cc_loading_image').hide();
console.log(response);
$('#CC_RESPONSE').show();
$('#CC_RESPONSE').html(response);
//here we would update the payment table - currently we will just refresh
post_string = '';
}
});
post_string = '';
}
else
{
//error
alert("Read Error");
$( "#cc-dialog-form" ).dialog( "close" );
}
Related
Add itemEditorValidatorFunction with Pop up window confirmation to Flexicious Grid
I am trying to have my Flexicious DataGrid ask for confirmation of a change when I click in a cell to edit a value and enter a new value which deviates from the original by a certain percentage. I cannot see an easy way to do this. Initially, I tried to write a itemEditorValidatorFunction, which returns a boolean. This works perfectly for a hard coded return value, but if I try to take the return value from the CloseEvent of an Alert, that value is ignored: protected function validateGcCap(editor:UIComponent):Boolean{ var warningBPDiffVal:Number = Number(5); var warningPerCentDiffVal:Number = Number(warningBPDiffVal / 1000); var allowChange:Boolean = true; var origGcCapVal:Number = Number(managerGrid.getCurrentEditingCell().text); var newGcCapVal:Number = Number((editor as TextInput).text); var diffVal:Number = Number(newGcCapVal - origGcCapVal); if (origGcCapVal > newGcCapVal) { diffVal = origGcCapVal - newGcCapVal; } if (diffVal > warningPerCentDiffVal) { //Alert.show("you changed the gccap from " + origGcCapVal + " to " + newGcCapVal + " by " + diffVal); function alertCloseHandler(event:CloseEvent):void{ if (event.detail == Alert.CANCEL) { allowChange = false; } }; var alert:Alert = Alert.show("Are you sure that you want to update gcCap% by more than " + warningBPDiffVal + "bps?", "Please Confirm", (Alert.OK | Alert.CANCEL), this, alertCloseHandler); } return allowChange; } I also tried to write a itemEditor for the grids:FlexDataGridColumn, where I extended com.flexicious.controls.TextInput, but I could not work out which method to override. I wanted to override the method and only make the call to super if the Alert was clicked OK, but I could not see which method I should override. I tried override protected function onTextInput(textEvent:TextEvent):void, but this did nothing. I would be grateful for any insight into this problem.
Not sure why someone decided to downvote your question, it seems quite valid. From looking at this, the best way for you would be to "undo" the edit when the user selects no on the box. If you have enableTrackChanges on, all you have to do is to remove that change from the dgGrid.changes collection and call dgGrid.refreshCells(). If you dont have enableTrackChanges, all you need to do is to update the dataProvider row with the old value, call dgGrid.refreshCells() and you should be set.
This is what works: private function validateGcCap(editor:UIComponent):Boolean{ var warningBPDiffVal:Number = Number(5); var cell:IFlexDataGridCell = managerGrid.getCurrentEditingCell(); var warningPerCentDiffVal:Number = Number(warningBPDiffVal / 1000); var origGcCapVal:Number = Number(cell.text); var newGcCapVal:Number = Number((editor as TextInput).text); var diffVal:Number = Number(newGcCapVal - origGcCapVal); if (origGcCapVal > newGcCapVal){ diffVal = origGcCapVal - newGcCapVal; } if (diffVal > warningPerCentDiffVal){ function alertCloseHandler(event:CloseEvent):void{ if (event.detail == Alert.CANCEL) { IAParamsVO(cell.rowInfo.data).gcCapWrapper = origGcCapVal; managerGrid.refreshCells(); } } Alert.show("Are you sure that you want to update gcCap% by more than " + warningBPDiffVal + "bps?", "Please Confirm", (Alert.OK | Alert.CANCEL), this, alertCloseHandler); } return true; }
Unsufficient privileges from responseText in Plone4.3
I use PloneBooking3.0.0a2 with Plone4.3.3, but if I want to show periodic bookings I get an unsufficient privileges error. In my opinion there are two functions responsible for that: function showPeriodicityResult(url, alt_url, target_id, form_id, waiting_text) { ajaxobject = getXmlHttpRequest(); form = document.getElementById(form_id); periodicity_type = getPeriodicityType(form); periodicity_end_date = form['periodicity_form_periodicity_end_date_0'].value; periodicity_variable = form['periodicity2_x'].value; query = getPeriodicityQuery(periodicity_type, periodicity_end_date, periodicity_variable); url = url + query + "&d=" + (new Date()).getTime(); alt_url = alt_url + query; // Opera does not support ajax if (ajaxobject == null) { window.location = alt_url; } else { var node = document.getElementById(target_id); node.innerHTML = waiting_text; ajaxobject.open('GET', url, true); ajaxobject.onreadystatechange = function(){CallBackGenerateAjaxHTML(ajaxobject, target_id);}; ajaxobject.send(null); } } and function CallBackGenerateAjaxHTML(ajaxobject, target_id) { if (ajaxobject.readyState == 4) { if (ajaxobject.status > 299 || ajaxobject.status < 200) { return; } elem = document.getElementById(target_id); elem.innerHTML = ajaxobject.responseText; } } Especially the innerHTML setting with responseText seems to be a problem. Is there is a quick answer like Plone version diff from 3 to 4 or must I work in-depth?
You mentioned in the comments that the portal.uid_catalog raises the Unauthorized. When I recall correctly the uid-catalog requires a higher permission since the last Plone hotfix. But you also can search an Item when given a UID with the normal Catalog. here_obj python:portal.portal_catalog(UID=here_uid)[0].getObject(); This way you should be able to get your Object.
Full calendar ResourceView horizontal and vertical axis
I'm using the fullcalendar resourceviews fork version 1.6.1.6... I used an older version which had the resources on the top and the times on the left axis. But now it is different. The times are on the top and the resources are on the left axis. It's not that good anymore. Is there a way to change it? I need the newer version of it because of the refetchResources function.
I modified the resource object (using Ike Lin fullCalendar) and added an array which includes the number of the day, start time and end time like 0 -> 09:00 -> 12:00, 1 10:00 -> 15:30 ... Then I changed the fullcalendar.js function updateCells() { var i; var headCell; var bodyCell; var date; var d; var maxd; var today = clearTime(new Date()); for (i=0; i<colCnt; i++) { date = resourceDate(i); headCell = dayHeadCells.eq(i); if(resources[i].anwesenheit[date.getDay()-1] != null){ var von = resources[i].anwesenheit[date.getDay()-1].von; var _von = von.substring(0, 5); var bis = resources[i].anwesenheit[date.getDay()-1].bis; var _bis = bis.substring(0, 5); headCell.html(resources[i].name + "<p style='font-weight: normal; font-size: 11px;'>" + _von + " - " + _bis + " Uhr</p>"); } else { headCell.html(resources[i].name); } headCell.attr("id", resources[i].id); bodyCell = dayBodyCells.eq(i); if (+date == +today) { bodyCell.addClass(tm + '-state-highlight fc-today'); }else{ bodyCell.removeClass(tm + '-state-highlight fc-today'); } setDayID(headCell.add(bodyCell), date); } } This shows the work time from each resource right unter the name of the resource. Also I added a serverside function to the select function which checks if the resource is available. If yes, then the event will be created, else the event won't be created and I get an error message. Now I can work with it. It's not exactly what I wanted, but it's nice to use now. It updates the times under the resource name on every day change so I have an overview when a resource is available and when it's not available.
google api .net client v3 getting free busy information
I am trying to query free busy data from Google calendar. Simply I am providing start date/time and end date/time. All I want to know is if this time frame is available or not. When I run below query, I get "responseOBJ" response object which doesn't seem to include what I need. The response object only contains start and end time. It doesn't contain flag such as "IsBusy" "IsAvailable" https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query #region Free_busy_request_NOT_WORKING FreeBusyRequest requestobj = new FreeBusyRequest(); FreeBusyRequestItem c = new FreeBusyRequestItem(); c.Id = "calendarresource#domain.com"; requestobj.Items = new List<FreeBusyRequestItem>(); requestobj.Items.Add(c); requestobj.TimeMin = DateTime.Now.AddDays(1); requestobj.TimeMax = DateTime.Now.AddDays(2); FreebusyResource.QueryRequest TestRequest = calendarService.Freebusy.Query(requestobj); // var TestRequest = calendarService.Freebusy. // FreeBusyResponse responseOBJ = TestRequest.Execute(); var responseOBJ = TestRequest.Execute(); #endregion
Calendar API will only ever provide ordered busy blocks in the response, never available blocks. Everything outside busy is available. Do you have at least one event on the calendar with the given ID in the time window? Also the account you are using needs to have at least free-busy access to the resource to be able to retrieve availability.
I know this question is old, however I think it would be beneficial to see an example. You will needed to actually grab the Busy information from your response. Below is a snippet from my own code (minus the call) with how to handle the response. You will need to utilized your c.Id as the key to search through the response: FreebusyResource.QueryRequest testRequest = service.Freebusy.Query(busyRequest); var responseObject = testRequest.Execute(); bool checkBusy; bool containsKey; if (responseObject.Calendars.ContainsKey("**INSERT YOUR KEY HERE**")) { containsKey = true; if (containsKey) { //Had to deconstruct API response by WriteLine(). Busy returns a count of 1, while being free returns a count of 0. //These are properties of a dictionary and a List of the responseObject (dictionary returned by API POST). if (responseObject.Calendars["**YOUR KEY HERE**"].Busy.Count == 0) { checkBusy = false; //WriteLine(checkBusy); } else { checkBusy = true; //WriteLine(checkBusy); } if (checkBusy == true) { var busyStart = responseObject.Calendars["**YOUR KEY HERE**"].Busy[0].Start; var busyEnd = responseObject.Calendars["**YOUR KEY HERE**].Busy[0].End; //WriteLine(busyStart); //WriteLine(busyEnd); //Read(); string isBusyString = "Between " + busyStart + " and " + busyEnd + " your trainer is busy"; richTextBox1.Text = isBusyString; } else { string isFreeString = "Between " + startDate + " and " + endDate + " your trainer is free"; richTextBox1.Text += isFreeString; } } else { richTextBox1.Clear(); MessageBox.Show("CalendarAPIv3 has failed, please contact support\nregarding missing <key>", "ERROR!"); } } My suggestion would be to break your responses down by writing them to the console. Then, you can "deconstruct" them. That is how I was able to figure out "where" to look within the response. As noted above, you will only receive the information for busyBlocks. I used the date and time that was selected by my client's search to show the "free" times. EDIT: You'll need to check if your key exists before attempting the TryGetValue or searching with a keyvaluepair.
Change color of past events in Fullcalendar
I'm trying to implement this solution to "grey out" past events in Fullcalendar, but I'm not having any luck. I'm not too well versed in Javascript, though, so I assume I'm making some dumb mistakes. I've been putting the suggested code into fullcalendar.js, inside the call for daySegHTML(segs) around line 4587. I added the first two lines at the end of the function's initial var list (Why not, I figured)—so something like this: ... var leftCol; var rightCol; var left; var right; var skinCss; var hoy = new Date;// get today's date hoy = parseInt((hoy.getTime()) / 1000); //get today date in unix var html = ''; ... Then, just below, I added the other two lines inside the loop: for (i=0; i<segCnt; i++) { seg = segs[i]; event = seg.event; classes = ['fc-event', 'fc-event-skin', 'fc-event-hori']; if (isEventDraggable(event)) { classes.push('fc-event-draggable'); } unixevent = parseInt((event.end.getTime()) / 1000); //event date in Unix if (unixevent < hoy) {classes.push('fc-past');} //add class if event is old if (rtl) { if (seg.isStart) { classes.push('fc-corner-right'); } ... Running this code results in a rendered calendar with no events displayed and an error message: Uncaught TypeError: Cannot call method 'getTime' of null The "null" being referred to is, apparently, event.end.getTime(). But I'm not sure I understand what exactly is going wrong, or how things are being executed. As written, it seems like it should work. At this point in the code, from what I can tell, event.end contains a valid IETF timecode, but for some reason it's "not there" when I try to run it through getTime()? This isn't a mission-critical tweak for me, but would still be nice—and I'd like to understand what's going on and what I'm doing wrong, as well! Any help greatly appreciated!
If you are using FullCalendar2 with Google Calendar, you will need to use the version of the code below. This uses Moment.js to do some conversions, but since FC2 requires it, you'll be using it already. eventRender: function(event, element, view) { var ntoday = new Date().getTime(); var eventEnd = moment( event.end ).valueOf(); var eventStart = moment( event.start ).valueOf(); if (!event.end){ if (eventStart < ntoday){ element.addClass("past-event"); element.children().addClass("past-event"); } } else { if (eventEnd < ntoday){ element.addClass("past-event"); element.children().addClass("past-event"); } } }
As per FullCalendar v1.6.4 Style past events in css: .fc-past{background-color:red;} Style future events in css: .fc-future{background-color:red;}
There's no need to fiddle with fullcalendar.js. Just add a callback, like: eventRender: function(calev, elt, view) { if (calev.end.getTime() < sometime()) elt.addClass("greyclass"); }, you just have to define the correct CSS for .greyclass.
Every event has an ID associated with it. It is a good idea to maintain your own meta information on all events based on their ids. If you are getting the events popupated from a backend database, add a field to your table. What has worked best for me is to rely on callbacks only to get the event ids and then set/reset attributes fetched from my own data store. Just to give you some perspective, I am pasting below a section of my code snippet. The key is to target the EventDAO class for all your needs. public class EventDAO { //change the connection string as per your database connection. //private static string connectionString = "Data Source=ASHIT\\SQLEXPRESS;Initial Catalog=amit;Integrated Security=True"; //this method retrieves all events within range start-end public static List<CalendarEvent> getEvents(DateTime start, DateTime end, long nParlorID) { List<CalendarEvent> events = new List<CalendarEvent>(); // your data access class instance clsAppointments objAppts = new clsAppointments(); DataTable dt = objAppts.SelectAll( start, end); for(int i=0; i<dt.Rows.Count; ++i) { CalendarEvent cevent = new CalendarEvent(); cevent.id = (int)Convert.ToInt64(dt.Rows[i]["ID"]); ..... Int32 apptDuration = objAppts.GetDuration(); // minutes string staffName = objAppts.GetStaffName(); string eventDesc = objAppts.GetServiceName(); cevent.title = eventDesc + ":" + staffName; cevent.description = "Staff name: " + staffName + ", Description: " + eventDesc; cevent.start = (DateTime)dt.Rows[i]["AppointmentDate"]; cevent.end = (DateTime) cevent.start.AddMinutes(apptDuration); // set appropriate classNames based on whatever parameters you have. if (cevent.start < DateTime.Now) { cevent.className = "pastEventsClass"; } ..... events.Add(cevent); } } } The high level steps are as follows: Add a property to your cevent class. Call it className or anything else you desire. Fill it out in EventDAO class while getting all events. Use database or any other local store you maintain to get the meta information. In your jsonresponse.ashx, retrieve the className and add it to the event returned. Example snippet from jsonresponse.ashx: return "{" + "id: '" + cevent.id + "'," + "title: '" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "'," + "start: " + ConvertToTimestamp(cevent.start).ToString() + "," + "end: " + ConvertToTimestamp(cevent.end).ToString() + "," + "allDay:" + allDay + "," + "className: '" + cevent.className + "'," + "description: '" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "'" + "},";
Adapted from #MaxD The below code is what i used for colouring past events grey. JS for fullcalendar pulling in Json events: '/json-feed.php', eventRender: function(event,element,view) { if (event.end < new Date().getTime()) element.addClass("past-event"); }, other options .... 'event.end' in my Json is a full date time '2017-10-10 10:00:00' CSS .past-event.fc-event, .past-event .fc-event-dot { background: #a7a7a7; border-color: #848484 }
eventDataTransform = (eventData) => { let newDate = new Date(); if(new Date(newDate.setHours(0, 0, 0, 0)).getTime() > eventData.start.getTime()){ eventData.color = "grey"; }else{ eventData.color = "blue"; } return eventData; } //color will change background color of event //textColor to change the text color
Adapted from #Jeff original answer just simply check to see if an end date exists, if it does use it otherwise use the start date. There is an allDay key (true/false) but non allDay events can still be created without an end date so it will still throw an null error. Below code has worked for me. eventRender: function(calev, elt, view) { var ntoday = new Date().getTime(); if (!calev.end){ if (calev.start.getTime() < ntoday){ elt.addClass("past"); elt.children().addClass("past"); } } else { if (calev.end.getTime() < ntoday){ elt.addClass("past"); elt.children().addClass("past"); } } }
Ok, so here's what I've got now, that's working (kind of): eventRender: function(calev, elt, view) { var ntoday = new Date(); if (calev.start.getTime() < ntoday.getTime()){ elt.addClass("past"); elt.children().addClass("past"); } } In my stylesheet, I found I needed to restyle the outer and inner elements to change the color; thus the elt.children().addclass addition. The only time check I could get to work, lacking an end time for all day events, was to look at the start time - but this is going to cause problems with multi-day events, obviously. Is there another possible solution?