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!

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

Reflection.js

I have been using a fancy snippet of JavaScript ever since I ran into it a few months back.  After giving it some time to prove itself, it’s time to thank Cow for making my blog, reflective.

What is it?
Reflection.js

What does it do?
Reflection.js allows you to add reflections to the images on your web pages using CSS. All you have to do is give the desired images a “reflect” CSS class. Simple as that.

I have used it numerous times, but here are a few more examples…

happy-halloween heart-day ice-cream-day

Whatcha think? Pretty cool right? If you want it? You can download it here… http://cow.neondragon.net/stuff/reflection/

-Thuan

Javascript: Month + Year

I recently needed to use javascript to write out the current month and year. Here, HTH…

<script type=”text/javascript”>
<!–
var currentMonthYear = new Date()
var monthNumber = currentMonthYear.getMonth() + 1
var year = currentMonthYear.getFullYear()
if (monthNumber==1) monthName=(‘January’);
else if (monthNumber==2) monthName=(‘February’);
else if (monthNumber==3) monthName=(‘March’);
else if (monthNumber==4) monthName=(‘April’);
else if (monthNumber==5) monthName=(‘May’);
else if (monthNumber==6) monthName=(‘June’);
else if (monthNumber==7) monthName=(‘July’);
else if (monthNumber==8) monthName=(‘August’);
else if (monthNumber==9) monthName=(‘September’);
else if (monthNumber==10) monthName=(‘October’);
else if (monthNumber===11) monthName=(‘November’);
else monthName=(‘December’);
document.write(monthName + ” ” + year)
//–>
</script>