By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

sql date to js

P: n/a
Hi,

is there a readily available function/script out there that would
convert a datetime value from MS SQL server to a date javascript can
understand.

something
var jsDate = new Date( func_SQL_TO_JS_Date(sqlDate) )

//where as sqlDate is a string of the format "yyyy-mm-dd hh:mm:ss.0"

Thanks

Sep 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
JRS: In article <11**********************@m7g2000cwm.googlegroups. com>,
dated Tue, 19 Sep 2006 08:32:48 remote, seen in
news:comp.lang.javascript, aa**********@yahoo.com posted :
>is there a readily available function/script out there that would
convert a datetime value from MS SQL server to a date javascript can
understand.

something
var jsDate = new Date( func_SQL_TO_JS_Date(sqlDate) )

//where as sqlDate is a string of the format "yyyy-mm-dd hh:mm:ss.0"
That's an ISO 8601 format.

Yes. Read the newsgroup FAQ; see below.

You should consider whether the SQL date/time is local or UTC, and
whether validation is needed. For unvalidated local, consider

function func_SQL_TO_JS_Date(sqlDate) {
return sqlDate.replace(/^(....).(..).(.{11}).*$/, "$1/$2/$3") }

For UTC, consider replacing $3 with $3Z or $3 UTC .

If you need to honour the fractional seconds treat them separately as
milliseconds and incorporate them after.

Alternatively, split the input into seven fields - .split(/\D/) - should
do it - and use new Date or Date.parse with six or seven arguments.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 19 '06 #2

P: n/a
JRS: In article <11**********************@m7g2000cwm.googlegroups. com>,
dated Tue, 19 Sep 2006 08:32:48 remote, seen in
news:comp.lang.javascript, aa**********@yahoo.com posted :
>
//where as sqlDate is a string of the format "yyyy-mm-dd hh:mm:ss.0"
BTW, that's a subset of an XML/.NET format, which you could have found
out how to deal with via the FAQ, at

<URL:http://www.merlyn.demon.co.uk/js-date3.htm#XML>

And ISO 8601 is in the previous section.

It's a good idea to read the newsgroup and its FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 20 '06 #3

P: n/a

<aa**********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hi,

is there a readily available function/script out there that would
convert a datetime value from MS SQL server to a date javascript can
understand.

something
var jsDate = new Date( func_SQL_TO_JS_Date(sqlDate) )

//where as sqlDate is a string of the format "yyyy-mm-dd hh:mm:ss.0"
My Date Extension library will convert that (now). ISO 8601 dates have the
date and time segments separated by a "T" - I just had to update it to also
accept a space.

The updated library is here:

http://www.depressedpress.com/Conten...DateExtensions

The method would be called as a static method of Date like this (adapting
your example):

Date.parseIso8601(sqlDate)

Hope this helps.

Jim Davis
Sep 21 '06 #4

P: n/a
JRS: In article <w6******************************@giganews.com>, dated
Wed, 20 Sep 2006 23:44:43 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
><aa**********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegr oups.com...
>something
var jsDate = new Date( func_SQL_TO_JS_Date(sqlDate) )

//where as sqlDate is a string of the format "yyyy-mm-dd hh:mm:ss.0"

My Date Extension library will convert that (now). ISO 8601 dates have the
date and time segments separated by a "T" - I just had to update it to also
accept a space.
Are you quite sure that segments are necessarily separated by a "T"?

In ISO 8601:2000 - I've studied a final draft - the T was mandatory.
But I've been led to believe that a space is now permissible - I've not
seen ISO 8601:2004. It would be a pity to mislead others.

On the assumption that the input string is supposed to contain exactly
one date-time with separated fields (but perhaps not exclusively), ISTM
appropriate to make at least the date separator wild (RegExp dot), and
perhaps to make the "T" position accept more characters (for example,
allowing for CR LF whitespace), as that only needs \D+ .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Sep 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.