?
Solved

VBScript and UK Dates

Posted on 1999-01-18
5
Medium Priority
?
338 Views
Last Modified: 2013-12-25
Using VBScript in Active Server Pages
How can I convert dates to UK Format -> dd:mm:yy
I don't want to convert to a string as I am querying an SQL database.  Basically I want it so that users of the site can enter the date in the UK format only, as they are used to.  I dont mind if VBscript converts to US Format behind the scenes.  This will even be useful for querying the SQL database.  The code that needs changing follows...I am collecting two dates.
if Request.Form("Date1") <> "" then
      date1 = Request.Form("Date1")
else
      date1 = #01/01/98#      'converts to a date
end if
if Request.Form("Date2") <> "" then
      date2 = Request.Form("Date2")
else
      date2 = Date
end if
0
Comment
Question by:robinM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
ruperts earned 40 total points
ID: 1859065
This works in vb....
mydate being your date...

MyStr = Format(MyDate, "dd/mm/yy")      

' Returns "Wednesday,Jan 27 1993".

0
 
LVL 6

Expert Comment

by:PBall
ID: 1859066
date is a very tricky subject since it is viewed in so many different variation.  If you know that the user is in UK and do not expect outside UK clients, I guess it's OK, but otherwise, it's always that ambiguity factor of which one is the month and which one is the date...

btw.  you really use : to separate the date elements? heh.

I'd suggest setting a reminder to the date field such as:

Birthday _____________ (dd:mm:yy)

or alternative:

Birthday  Day ___ Month ___ Year ____

Since you are using ASP, I might as well suggest this: try ASP Calendar Control, it's available at http://www.fortunecity.com/meltingpot/headfort/175/asp/control/calendar/

date validation routine in vbscript:

function isUKDate( dt )
'assuming delimited by :

  dim v, d, m, y, l, i

  l = ":"

  v = trim(dt)

  i = instr(v,l)

  'delimiter not found, exit function
  if (i = 0 or isNull(i))
    isUKDate = false
    exit function
  end if

  d = trim(left(v,i-1)) 'retrieve the date value

  v = mid(v,i+1) 'chop off the day part

  i = instr(v,l)

  'delimiter not found, exit function
  if (i = 0 or isNull(i)) then
    isUKDate = false
    exit function
  end if
 
  m = trim(left(v,i-1)) 'get the month

  v = mid(v,i+1) 'chop off the month

  y = cLng(trim(v)) 'get the year part

  m = cInt(m)

  if m < 1 or m > 12 then
    isUKDate = false
    exit function
  end if

  i = monthlength(m)

  d = cInt(d)

  if d < 1 or d > i then
    isUKDate = false
    exit function
  end if

  isUKDate = true
end function

function monthlength( m, y )
  dim ml
  if m = 1 or m = 3 or m = 5 or m = 7 or _
     m = 8 or m =10 or m = 12 then
    ml = 31
  else
    if m = 2 then            'handle february
      if isLeapYear(y) then
        ml = 29
      else
        ml = 28
      end if
    else
      ml = 30
    end if
  end if

  monthlength = ml
end function

function isLeapYear( year )
  'it is leap year when the year is divisible by 4
  'but not divisible by 100,
  'but if the year is divisible by 400 then it is.

  isLeapYear = ((year mod 4 = 0) and (year mod 100 <> 0)) or (year mod 400 = 0)

end function

to use it:

  if isUKDate(document.form.datefield.value) then
    msgbox "Splendid.  You have entered the date correctly. Well done."
  else
    msgbox "Sorry, old chap.  I would suggest you to go back and study back how the date is supposed to be entered here in UK."
  end if

0
 
LVL 6

Expert Comment

by:PBall
ID: 1859067
rupert, format does not exists in vbscript.
formatdate does and that is not how you would use it either.

and that will only display the date, not validate whether or not the date entered is in UK/US format.
0
 
LVL 4

Expert Comment

by:ruperts
ID: 1859068
I'd remove my answer, if I could!
0
 

Author Comment

by:robinM
ID: 1859069
PBall, thanks for the CheckValidity function

I worked it out the answer myself after much hassle, here it is
Converting to UK
dateselected = Day(date()) &"/" &Month(date()) &"/" &Right(Year(date()), 2)

Converting back to US (for putting into SQL for example)
USDate = Month(dateselected) &"/" &Day(dateselected) &"/" &Right(Year(dateselected), 2)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

765 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