[Last Call] Learn how to a build a cloud-first strategyRegister Now


JavaScript DateTime vs .NET Date Time

Posted on 2006-04-15
Medium Priority
Last Modified: 2012-06-21

I am writing a WinForms application in C#. It hosts a Web Browser control that will display offline HTML content.

I need to calculate the following:
 - Day of the year (1 to 366: 366 for leap years)
 - Week of the Year (1 to 52: Didn't check but can some years have a 53rd week?)

Considering that I have access to the DOM, an easy way to do this is what I have done right now:
 - Place markers in the JavaScript code e.g. (var dayOfYear = ~~~DAYOFYEAR~~~;)
 - Before loading the HTML, replace the markers with calculated values from C# (html = html.Replace("~~~DAYOFYEAR~~~", System.DateTime.Now.SomeProperty);)
 - Load the HTML through the DOM

I'm doing this only because I don't have too much experience with JavaScript. I did check the Date class but did not find any direct ways to calculate this. Any ideas?

Out of context:
Also, instead of saving loading HTML files directly from the hard drive, I load the URL "abount:blank" and set set the document.body.innetHTML at runtime. This way, users cannot access the HTML/JavaScript code by doing a "View Source". This approach works fine until relative image paths are used. Since the file loaded does not have a valid location, should I use absolute image paths? Is that the best approach?


Question by:raheelasadkhan
  • 5
  • 2
LVL 11

Expert Comment

ID: 16460454
For the out of context question, that's an interesting idea.  How about starting off (in the head, or top of the body, or even just before the firs relative path):

<base href="http://www.yourdomain.com/zero_or_more/directories/">

Useful, if you've never encountered it before.  :-)

Peace and joy.  mvan
LVL 11

Expert Comment

ID: 16460464
... By the way, base href= works for loading .js files too!  Peace and joy.  mvan
LVL 11

Accepted Solution

mvan01 earned 2000 total points
ID: 16460523
For the weekofyear in JavaScript, check this.  I'll bet you can get the day of year out of the calc. too.  :-)


Peace and joy.  mvan
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 16462005
@mvan10: Regading the relative issue, I've nver come across this approach before and it sounds good. Will give it a try and let you know.

With respect to the real question, I have already considered the approachyou mentioned and was wondering if JavaScript offers an easier way to do this...
LVL 11

Expert Comment

ID: 16462054
Nah, don't think so.  Maybe over a few days I can search around for a tight JavaScript routine, but that one seems typical.

Peace and joy.  mvan

Author Comment

ID: 16462075
Thanks again. Calculating the values with math is no issue. I'll figure that out. I was wondering if there was built in support for this kind of stuff in JavaScript.
LVL 11

Expert Comment

ID: 16462679
Hi Khan,

You are welcome.  I'm glad I could help a bit.  JavaScript does not have the built in support.  If you've little experience with JS, then have a look here.  This page is for the Date properties, but is an excellent reference site for JS programming:


And as I said, the base href= is quite useful, esp. for testing code, but also to short-hand the otherwise req'd absolute paths.  You can even switch from one to another throughout the page, if need be.

I hope your project works out well.  Peace and joy.  mvan

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question