JSON to Javascript Date & Time (HH:MM:SS AMPM) 12HR

Recently needed to convert JSON to Javascript Date & Time. Not only that I needed it in a specific format (HH:MM:SS AMPM) 12HR. The answer wasn’t so clear cut and simple, but I found what worked for me:

var dateAdded = new Date(parseInt(val.DateAdded.replace("/Date(", "").replace(")/", ""), 10));
//the below 3 were other options
//var dateAdded = new Date(+val.DateAdded.replace(/\/Date\((\d+)\)\//, '$1'));
//var dateAdded = new Date(parseInt((val.DateAdded.substr(6))));
//var dateAdded = eval(val.DateAdded.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
$(".spnDate", table).html(formatDate(dateAdded));


function formatDate(dateAdded) {
var actDate = new Date(dateAdded);
var m = actDate.getMonth();
var d = actDate.getDate();
var y = actDate.getFullYear();
var h = actDate.getHours();
var mm = actDate.getMinutes();
var s = actDate.getSeconds();
var ampm = h >= 12 ? 'PM' : 'AM';
h = h % 12;
h = h ? h : 12; // the hour '0' should be '12'
mm = ('0' + mm).slice(-2); // the minute '0' should be '00'
s = ('0' + s).slice(-2); // the second '0' should be '00'
return (m + 1) + "/" + d + "/" + y + " " + h + ":" + mm + ":" + s + " " + ampm;
}

Hope this helps someone else!

DNN 7.4.2 Help Icon Error/Fix

I ran into a a weird bug from a fresh install of DNN 7.4.2. The help icon wouldn’t move. After a little digging, I tracked it down to the default portal’s default.css:

/*.dnnTooltip .dnnFormHelpContent span:after,
.dnnHelperTip .dnnFormHelpContent span:after {
position: absolute;
content: "";
left: 15px;
bottom: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid #000;
}*/

.dnnTooltip .dnnFormHelpContent .dnnHelpText {
word-wrap: break-word;
}

.bottomArrow:after
{
position: absolute;
content: "";
left: 15px;
bottom: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid #000;
}
.topArrow:before {
position: absolute;
content: "";
left: 15px;
top: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-bottom: 7px solid #000;
}

It seems the old tool tip css, was commented out and a new section put in. It wasn’t working for me so I just reverted things. I hope this didn’t break anything, and for now I hope it helps someone else:

.dnnTooltip .dnnFormHelpContent span:after, .dnnHelperTip .dnnFormHelpContent span:after {
position: absolute;
content: "";
left: 15px;
bottom: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid #000;
}

/*
.dnnTooltip .dnnFormHelpContent .dnnHelpText {
word-wrap: break-word;
}

.bottomArrow:after
{
position: absolute;
content: "";
left: 15px;
bottom: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid #000;
}
.topArrow:before {
position: absolute;
content: "";
left: 15px;
top: -7px;
width: 0;
height: 0;
opacity: 0.75;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-bottom: 7px solid #000;
}
*/

DESC Container.ItemIndex

Recently had a need to reverse the count for an asp.net Repeater for one of my pages.

My google searches: DESC Container.ItemIndex, reverse repeater count, ItemIndex(), etc.

Much of the code I found either didn’t fit into my code or required behind code rework that I wasn’t willing to do until necessary.

My last search: start with last Container.ItemIndex, landed me here: http://bit.ly/2nfLQMw

Again I had to modify the code a little, but this worked for me…


< %# (-1 * (Container.ItemIndex - DirectCast(DirectCast(Container.Parent, Repeater).DataSource, IList).Count)) %>

I hope someone else can use it!

Javascript, Query, & Dotnetnuke Postback

Free time… many coding projects came to mind. One in particular was highlighting all desired text on a certain page. I found nice JavaScript snippet here:

http://www.nsftools.com/misc/SearchAndHighlight.htm

//<script language=”JavaScript”>
function doHighlight(spanText, searchTerm, highlightStartTag, highlightEndTag) {
// the highlightStartTag and highlightEndTag parameters are optional
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = “<font style=’background-color:yellow;’>”;
highlightEndTag = “</font>”;
}
var newText = “”;
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcSpanText = spanText.toLowerCase();
while (spanText.length > 0) {
i = lcSpanText.indexOf(lcSearchTerm, i + 1);
if (i < 0) {
newText += spanText;
spanText = “”;
} else {
// skip anything inside an HTML tag
if (spanText.lastIndexOf(“>”, i) >= spanText.lastIndexOf(“<“, i)) {
// skip anything inside a <script> block
if (lcSpanText.lastIndexOf(“/script>”, i) >= lcSpanText.lastIndexOf(“<script”, i)) {
// skip anything inside a <HyperLink> block
if (lcSpanText.lastIndexOf(“/asp:HyperLink>”, i) >= lcSpanText.lastIndexOf(“<asp:HyperLink”, i)) {
// skip anything inside a <linkbutton> block
if (lcSpanText.lastIndexOf(“/asp:linkbutton>”, i) >= lcSpanText.lastIndexOf(“<asp:linkbutton”, i)) {
// skip anything inside a <Checkbox> block
if (lcSpanText.lastIndexOf(“/asp:Checkbox>”, i) >= lcSpanText.lastIndexOf(“<asp:Checkbox”, i)) {
// skip anything inside a <panel> block
if (lcSpanText.lastIndexOf(“/asp:panel>”, i) >= lcSpanText.lastIndexOf(“<asp:panel”, i)) {
// skip anything inside a <legend> block
if (lcSpanText.lastIndexOf(“legend>”, i) >= lcSpanText.lastIndexOf(“<legend”, i)) {
newText += spanText.substring(0, i) + highlightStartTag + spanText.substr(i, searchTerm.length) + highlightEndTag;
spanText = spanText.substr(i + searchTerm.length);
lcSpanText = spanText.toLowerCase();
i = -1;
}
}
}
}
}
}
}
}
}
return newText;
return false;
}
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
if (treatAsPhrase) {
searchArray = [searchText];
} else {
searchArray = searchText.split(” “);
}
if (!document.getElementById(“spanText”) || typeof (document.getElementById(“spanText”).innerHTML) == “undefined”) {
if (warnOnFailure) {
Sorry, for some reason the text of this page is unavailable. Searching will not work.”);
}
return false;
}
var spanText = document.getElementById(“spanText”).innerHTML;
for (var i = 0; i < searchArray.length; i++) {
spanText = doHighlight(spanText, searchArray[i], highlightStartTag, highlightEndTag);
}
document.getElementById(“spanText”).innerHTML = spanText;
return false;
}
function badphrases(){
highlightSearchTerms(‘die kill’);
}
//window.onload(setTimeout(‘badphrases();’,3000));
//</script>

The code worked beautifully when I tested it’s search then highlight functions. BUT when I tested it against the rest of the page I realized that my buttons weren’t working anymore. Something was going wrong and after playing with how the script loaded, what to block it from replacing, and where it highlighted I gave up.

Another solution had to be found. Knowing that Dotnetnuke had jQuery integrated into it I tried this jQuery plugin:

http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

Ostensibly, it looked like a winner like the last one. NO!, something was still wrong. Further inspection told me that the JavaScript was messing with DNN’s method of posting back. After some more trial and error I went to the DNN forums:

http://www.dnnsoftware.com/forums/forumid/199/threadid/503867/scope/posts

javascript and asp.net postbacks are somewhat at odds with each other under certain circumstances.
This is especially the case if there are any async / partial update panels on the page
– something that is more and more common these days.

That’s what I got, though it didn’t help much. I told me that I shouldn’t give up and after a little more searching I found my code: http://bartaz.github.io/sandbox.js/jquery.highlight.html

<style>.highlight { background-color: yellow; }</style>

<script language=”javascript” type=”text/javascript”>

jQuery.fn.highlight = function(pat) {
function innerHighlight(node, pat) {
var skip = 0;
if (node.nodeType == 3) {
var pos = node.data.toUpperCase().indexOf(pat);
if (pos >= 0) {
var spannode = document.createElement(‘span’);
spannode.className = ‘highlight’;
var middlebit = node.splitText(pos);
var endbit = middlebit.splitText(pat.length);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
}
}
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
for (var i = 0; i < node.childNodes.length; ++i) {
i += innerHighlight(node.childNodes[i], pat);
}
}
return skip;
}
return this.length && pat && pat.length ? this.each(function() {
innerHighlight(this, pat.toUpperCase());
}) : this;
};

$(document).ready(function(){
$(“td”).highlight(“kill”);
});

</script>

Thank you Bartaz and Johann for the nice code. Bartaz’s updated code did the trick. I hope this helps someone else working with Dotnetnuke. Happy Coding to all!

 

Working with Cron Jobs

For a while now, I have been using my shared hosting to backup/store numerous image files. Retrieving those files over the internet has always been a pain. Especially when there are hundreds of files in a given folder. The end goal is to be able to download the archived folder to my computer for easy viewing.

So, I took the time to code something up. Here’s the psuedo code:

  1. zip folder
  2. rename zip to include date stamp
  3. move zip to another destination
  4. delete contents of folder for future backups
  5. give myself a high five

Of course, I Google’d and looked for an easy way out… to see if some has posted the exact code I was looking for. No such luck but here are the links that were handy:

What I learned:

  • Cron Jobs on shared web hosting gives us the ability to run automated shell level scripts.
  • Cron Jobs coding was essentially Unix, nice I can code Unix!

Once I got started, I did run into a few Cron Jobs specifics:

  1. you have to escape the ‘%’ sign with a ‘\’ in a cron job, http://www.webhostingtalk.com/showthread.php?t=678062
  2. bonus: && operator, helped me reduce my script to one line, http://forums.freebsd.org/showthread.php?t=30964

Nevertheless, here’s the script that will hopefully help someone else looking to compress and backup a whole directory then delete its contents:

zip /destination/of/directory/with/date/label/$(date +\%Y\%m\%d).zip /directory/to/be/zipped/* && rm /directory/to/be/zipped/cleared/*

Happy Coding!

Bad GoDaddy Customer Service

Last night, I noticed a couple of my sites were down, both hosted on GoDaddy. After talking to support for about an hour and hearing that it was my fault. I did my own check up and restarted the application pool for ASP.net. That worked.

But this post is not about there servers randomly going down. It’s about another bad customer service experience I had with GoDaddy support. Every time I asked for the support expert (ha) to check to see if something had gone wrong on their end he said, “nothing has gone wrong, your site is scripted wrong.” Even after explaining that I had not touched my site in ages and the other was install by GoDaddy… he continued to disregard any my suggestion/queries has to what might have gone wrong.  In the end all I wanted was the sites up and not to point a finger.

I even started a post on their forums and without surprise other users have been experiencing the same problems for 30+ hours. Honestly they could have just checked and might have found/suggested something like “please try restarting your application pool?!?!”

http://community.godaddy.com/groups/web-hosting/forum/topic/sites-down-1/

It gets worst they edited any (if not) all the comments on the forums that made the server failures their fault. Again saying it’s our fault. Big time, NO NO!!!

After writing this I will still keep the sites mention with them because moving the sites will be such a hassle and their prices are one of the lowest for Windows hosting. But as for future sites and any potential customers they might have had going to them from me… the count is now ZERO.

So if you are considering GoDaddy web hosting. Be very very careful.

[rating:2/5]

-Thuan

DNN RADeditor Config’ing

A few weeks back I updated all my DNN sites to 5.4.4. Primarily to take advantage of ASP.net 3.5, but I also needed to come to terms with the fact that skins are now treated as modules. There were many other changes, one that came as a surprise was that FCKeditor was no longer DNN’s default editor. Telerik’s RADeditor was the new big guy in town and works just as well, but better in that it has more tools. The most useful tool that I have already taken advantage of is it’s spell checker. With that said, can there be too many tools? Tools that some of us will never use? YES!!! This is where the RADeditor’s configurability comes in handy. If you are looking to remove some of the tools from the default configuration, then edit this file…

DNN/Providers/HtmlEditorProviders/Telerik/Config/ToolsDefault.xml

I suggest that you only comment out the tools that you don’t want, you might find them handy later on…

If you look within the DNN/Providers/HtmlEditorProviders/Telerik/ folder you will also find the EditorOverride.css file. Which I found I needed to edit because by default the editor used the same “imaged background” and “centered” properties the site used.

Finally, if you find that you don’t like Telerik’s RADeditor, you can always revert back to the original FCKeditor via the web.config file…

find:
<htmlEditor defaultProvider=”TelerikEditorProvider“>
… change it to:
<htmlEditor defaultProvider=”FckHtmlEditorProvider“>

Happy Coding,
Thuan

HTML Back-A-Page

Programming to have your HTML go back a page isn’t hard. It’s simple Javascript that you can use in a link or a form’s button…

<form method="post">
<input type="button" value="Back" OnClick="history.go(-1 );return true;">
</form>

<a href="javascript:history.go(-1)">Back</a>

Hope that helps,
Thuan

HTML Tags Converter

For some reason I couldn’t find a HTML Tags Converter on the web at all, so I wrote one…

It’s really simple Javascript, but a very useful tool when you need it… if you find it useful, leave a comment. 🙂
-Thuan

Removing MS SQL Full Text Search

This week I had to move one of my sites from web host to another. Full Text Search, though useful, turned out to be a nuance because the new host doesn’t not allow it and the old host wanted to charge me $20 to remove it.

Why didn’t I just remove it myself? Well, I assumed I didn’t have the privileges because it was grayed out in Enterprise Manager. But hope was not lost, I fired up Query Analyzer and found that I was able to manage full text search functionality without any issues. Here’s the code if someone needs it…

Step 1, Drop the table (or tables if you have multiple)

USE DB_NAME;
GO
EXEC sp_fulltext_table 'TABLE_NAME', 'drop';
GO

Step 2,  Drop the Full Text Search Catalog

USE DB_NAME;
GO
EXEC sp_fulltext_catalog 'CATALOG_NAME', 'drop';
GO

In summary, working on a shared hosting environment can have its disadvantages but with a little Transact-SQL know-how it isn’t all that bad.

BTW, here’s a good reference for MS SQL Full Text Search Stored Procedures… linky.