Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2005

Tip: Looking for answers? Try searching our database.

Need to convert from .NET DateTime to JScript Date.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Varn - 22 Feb 2005 16:21 GMT
I have a .NET DateTime that I need to send to a JavaScript client and have
the JavaScript client create a Date() object from the .NET DateTime object.
I need to pass the DateTime object by string to the JavaScript client.  The
JavaScript Date() object can parse date strings, however there is no way to
parse a date string with the millisecond portion.  I also noticed that the
JavaScript Date() object accepts a UTC millisecond for its constructor as
well.  However, I can't figure out how to convert a .NET DateTime to a UTC
millisecond representation that can be passed to the JavaScript Date()
object.

Does anyone know how I can make this conversion happen?

I thought about parsing the string myself in JavaScript, but it seems like
there should be an easier way to do this.

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Kit George (MSFT) - 02 Mar 2005 17:26 GMT
There's bound to be a standard DateTime format we product, that JScript can
consume. Check out the title 'standard date and time format strings' in the
docs for the traditional formats, but I would suggest you try a format such
as 'u', 'r', or 's':

DateTime.Now.ToString("u")

Regards,
Kit
Ken Varn - 03 Mar 2005 18:42 GMT
None of the formats you mentioned include milliseconds.  The only way I have
been able to do it is to manually produce a date string using "yyyy/MM/dd
HH:mm:ss.fff".  However, the JScript Date() object constructor that accepts
strings only seems to be able to read strings that do not contain
milliseconds.  So... I end up having to manually parse this string in
JScript to extract the individual components and then build a JScript Date()
object by passing in month, day, year, etc.. to the alternate Date()
constructor.  I wish there was a more straight-forward way of doing this.  I
made a JScript function that does it:

function ExtractDate(Text)
{
  return new Date(parseInt(Text.substr(0,4),10),
                  parseInt(Text.substr(5,2),10)-1,
                  parseInt(Text.substr(8,2),10),
                  parseInt(Text.substr(11,2),10),
                  parseInt(Text.substr(14,2),10),
                  parseInt(Text.substr(17,2),10),
                  parseInt(Text.substr(20,3),10));
}

This is sort of clunky, but it works.  I just wish there was a more elegant
translation between the two types.

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

> There's bound to be a standard DateTime format we product, that JScript can
> consume. Check out the title 'standard date and time format strings' in the
[quoted text clipped - 5 lines]
> Regards,
> Kit
Ryan Walberg [MCSD] - 18 Mar 2005 20:53 GMT
> I have a .NET DateTime that I need to send to a JavaScript client and have
> the JavaScript client create a Date() object from the .NET DateTime object.
[quoted text clipped - 10 lines]
> I thought about parsing the string myself in JavaScript, but it seems like
> there should be an easier way to do this.

The JavaScript Date object has a constructor like this:

new Date( yr_num, mo_num, day_num [, hr_num, min_num, sec_num, ms_num])

Use that one.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.