Reputation: 223
I was hoping someone could shed some light on this topic for me, I was asked to add a Countdown to a specific date, on a customers SharePoint home page. I most often develop in Visual Studios, while SharePoint is mostly a new topic to my portfolio after I created the countdown, I soon found myself lost in a flurry of information.
My cs file consist of this:
namespace TimerCountdown
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Get these values however you like.
DateTime daysLeft = DateTime.Parse("12/29/2016 02:00:01 AM");
DateTime startDate = DateTime.Now;
//Create A Timer
Timer tmr = new Timer();
tmr.Interval = 500;
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Enabled = true;
tmr.Start();
//Calculate countdown timer.
TimeSpan t = daysLeft - startDate;
string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", t.Days, t.Hours, t.Minutes, t.Seconds);
label1.Content = countDown;
}
void tmr_Tick(object sender, EventArgs e)
{
DateTime daysLeft = DateTime.Parse("12/29/2016 02:00:01 AM");
DateTime startDate = DateTime.Now;
TimeSpan ts = daysLeft.Subtract(startDate);
label1.Content = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
}
}
}
And this is where I get a little confused, I've read post of folks saying use ASP and add the code behind in there. Others say no don't do it that way. Some say upload your files with a line of code, which brings up topics of security. I'd like to reach out to this community in hopes of someone being able to shed some light/Add links to a good information website for this topic or if even someone knows of a great walk-through or even point out that my approach is totally wrong.
As I feel this might be all confusing to some users - The Customer asked for. A simple label on the SharePoint's home page banner with a code that shows a countdown on load. Any feed back is appreciated
Upvotes: 0
Views: 65
Reputation: 7090
Your don't need the C# code, you can do all with simple javascript and use the Html WebPart.
Here the code for a simple page give it a try.
<!DOCTYPE html>
<html>
<head>
<title>Countdown</title>
</head>
<body>
<script>
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
var dt = new Date(2016, 12, 31, 23, 59, 59);
setInterval(function(){
var today = new Date();
var end = Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate());
var start = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());
var days = Math.floor((end - start) / _MS_PER_DAY);
var s = days + " Days, " + (dt.getHours() - today.getHours()) + " Hours, " + (dt.getMinutes() - today.getMinutes())+ " Minutes, " + (dt.getSeconds() - today.getSeconds()) + " Seconds til launch";
document.getElementById("cdwLabel").innerHTML = s;
}, 1000);
</script>
<span id="cdwLabel"></span>
</body>
</html>
Upvotes: 1