473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

date conv

I have a data like:

03-Jul-05

and need to convert it to:

7/3/2005

Is there a function for this already?

Jul 28 '05 #1
4 3548
mike wrote:
I have a data like:
03-Jul-05
and need to convert it to:
7/3/2005
Is there a function for this already?


There are a number of date function libs around.
Using mine from http://www.javascripttoolbox.com/date/ you could simply do:

var d = "03-Jul-05";
var formatted = formatDate(new
Date(getDateFromFormat(d,"d-MMM-yy")),"M/d/yyyy");

If your case is very specific, you could surely avoid using a big library
and instead use regular expressions to perform the task. Less code, but also
less flexibility. Depends on what you really need.

PS: Ignore John Stockton's comment which is surely coming, complaining about
converting from a good format to a dumb "American" one. :)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 28 '05 #2
yeah, i'm trying to understand what is going on in that script, not not
just plug a bunch of code in my page without learning anything.

thanks

Jul 29 '05 #3
JRS: In article <dc********@news1.newsguy.com>, dated Thu, 28 Jul 2005
14:44:20, seen in news:comp.lang.javascript, Matt Kruse
<ne********@mattkruse.com> posted :
mike wrote:
I have a data like:
03-Jul-05
and need to convert it to:
7/3/2005
Is there a function for this already?
There are a number of date function libs around.
Using mine from http://www.javascripttoolbox.com/date/ you could simply do:

var d = "03-Jul-05";
var formatted = formatDate(new
Date(getDateFromFormat(d,"d-MMM-yy")),"M/d/yyyy");

If your case is very specific, you could surely avoid using a big library
and instead use regular expressions to perform the task. Less code, but also
less flexibility. Depends on what you really need.


Since he indicated an exact input format, it is reasonable to assume
that anything else is impossible or erroneous.

PS: Ignore John Stockton's comment which is surely coming, complaining about
converting from a good format to a dumb "American" one. :)


It's nice to see that at least one American here recognises that the US
format is dumb. There can be no doubt that M/D/YYYY is American; FFF is
used nowhere outside the unofficial American Empire (presumably much of
Iraq is now infested with it). So his quotes around 'American' are not
needed : OTOH, because he did not include 'dumb' within the quotes, it
is clear that he accepts the description 'dumb'.
To do the job only with a RegExp requires the use of a function in the
replacement. This looks simpler :

d = "03-Jul-05"
A = d.replace(/0?([1-9]?\d)-(\w\w\w)-(\d\d)/, "/$1/20$3")
A = "xxxJanFebMarAprMayJunJulAug...".indexOf(RegExp.$2 )/3 + A

Use of RegExp.$2 may be deprecated, but it will no doubt be possible to
avoid it.

Note that malformed input may give surprise results; if the input is not
assuredly [#]#-Mon-##, checks will be needed.

Note that the above could be easily adapted to provide Y M D as numbers,
which could easily be loaded into a Date Object, validated, and output
in a programmer-defined manner. See my pages js-date3.htm etc.

Eschew bloatware; it imposes an unnecessary burden on everyone who
downloads the code, which means all readers of pages using it. How many
lines do formatDate and getDateFromFormat need?

--
© 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 29 '05 #4
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Thu, 28 Jul 2005 17:25:34, seen in news:comp.lang.javascript,
mike <hi****@charter.net> posted :
yeah, i'm trying to understand what is going on in that script, not not
just plug a bunch of code in my page without learning anything.


Don't bother with bloatware, then.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
3
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
1
by: Liz Malcolm | last post by:
Hello and TIA. I have a DE form with an option group that if daily is selected todays date is used for start and end date, if weekly is selected Monday - Friday is used. I am trying to add a...
1
by: A.Dagostino | last post by:
i have in my page an input text with a date like this "18/10/2005" i need to convert this value to lunch a select on SQL2000 like this "WHERE (dataAssunzione = CONVERT(DATETIME, '2005-10-18...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.