TIP: parsing partial dates

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

TIP: parsing partial dates

Postby antonio » Fri May 26, 2006 12:51 am

Here's a Java method to parse pseudo dates. Suggestions and improvements welcome! Happy to learn if there's a more efficient way.

Note that this returns dd-MM-yyyy dates, will need to be modified for US format.

Pseudo dates are partial dates, like the day only, or the day and month, with or without punctuation; or a short cut such as 'Y' for yesterday. It speeds up entry of dates, good for people who like keyboard entry with the minimum of keys.

EG
5 => 05-05-2006 (if it's May 2006)
0404 => 04-04-2006
010558 => 01-05-1958
3/4 => 03-04-2006


Enter the pseudodate into a text field (not a datetime field)

In this example,
date_of_service_pseudo is the text element for data entry and display.
date_of_service is a datetime field, that doesn't need to be an element on the form (and may not be needed at all - but handy if the date is used in calcs and relations)

This is the form method parseDateOfService called with the OnDataChange property

Code: Select all
// parse the pseudo date and put it into the real date field
date_of_service = globals.parsePseudoDate(date_of_service_pseudo)
// and then put the correctly formatted string back into the text field
date_of_service_pseudo = utils.dateFormat(date_of_service, 'dd-MM-yyyy')



This is a global method parsePseudoDate

Code: Select all
// Take a string in the following forms and convert to a valid date
// Works for European format dates. Needs modification if used for US dates.

// d or dd    - adds the current month and year
// ddmm or dd-mm or dd/mm or dd.mm or dd mm   - adds the current year
// ddmmyy or dd-mm-yy or dd/mm/yy or dd.mm.yy or - adds the appropriate century in the last 99 years.
// ddmmyyyy etc as expected.

// 'T' or 't' = today's date
// 'Y' or 'y' = yesterday's date.

// NOTE if century is not given
//   dates in the near future (this year or next) will default to the 20xx, (see lines 50 and 80)
//   everything else defaults to a past year, so 10.01.10 => 10/01/1910, 010101 => 01-01-2001
//   because I mostly use this for date of birth,
//   and it is relatively rare in my app to enter a date more than 12 months in the future

var ds = arguments[0]        //ds = date string
var df = null            //df = date format
var dd = application.getTimeStamp().getDate()
var mm = application.getTimeStamp().getMonth()
var yyyy = application.getTimeStamp().getYear()+1900
var yy = 0

if (ds.toUpperCase() == "T")
{
   df = application.getTimeStamp()
}
else if (ds.toUpperCase() == "Y")
{
   df = application.getTimeStamp()-86400000 // number of milliseconds in a day.
}

else if ( plugins.it2be_tools.isInteger(ds)) // no punctuation
{
   if (ds>0 && ds <=31)
   {
      // d or dd only
      df = new Date(yyyy, mm, ds)
   }
   else if (ds.length == 4)
   {
      //ddmm, and adjust mm for Java where Jan = 0 and Dec = 11
      df = new Date(yyyy, (utils.stringRight(ds , 2)/1-1), utils.stringLeft(ds, 2) )
   }
   else if (ds.length == 6)
   {
      //ddmmyy
      yy = utils.stringRight(ds , 2)/1 + 2000
      if (yy - yyyy > 1) {yy = yy - 100}
      df = new Date(yy, (utils.stringMiddle(ds, 3, 2)/1-1), utils.stringLeft(ds, 2))
   }
   else if (ds.length == 8)
   {
      //ddmmyyyy
      df = new Date(utils.stringRight(ds , 4), (utils.stringMiddle(ds, 3, 2)/1-1), utils.stringLeft(ds, 2))
   }
   else // 3, 5, 7, 9+ digits are ambiguous or invalid.
   {
      application.beep()
      return null
   }
}
else // replace punctuation with spaces, for word count
{
   ds = utils.stringReplace(ds, '-', ' ')
   ds = utils.stringReplace(ds, '/', ' ')
   ds = utils.stringReplace(ds, '.', ' ')
   ds = utils.stringReplace(ds, ',', ' ')
   if (utils.stringWordCount(ds) == 2)
   {
      df = new Date(yyyy, (utils.stringRightWords(ds , 1)/1-1), utils.stringLeftWords(ds, 1) )
   }
   else if (utils.stringWordCount(ds) == 3)
   {
      yy = utils.stringRightWords(ds , 1) // could be 1, 2, or 4 digits, hopefully not 3!
      if (yy < 100)
      {
         yy = yy/1 + 2000
         if (yy - yyyy > 1) {yy = yy - 100}
      }
      df = new Date(yy, (utils.stringMiddleWords(ds, 2, 1)/1-1), utils.stringLeftWords(ds, 1) )
   
   }
   else
   {
      application.beep()
      return null
   }
}
return df // to return a date object to go into a datetime field.

// or if you want to return it as a string to go back into the date_mask text field use this return instead, with a suitably modified form method.
//ds = utils.dateFormat(df, 'dd-MM-yyyy') // can change the format here.
//return ds


To include a popup calendar on the form, add a button element next to the pseudo date element, with the text '...' calling a form method

Code: Select all
date_of_service_pseudo = utils.dateFormat(application.showCalendar(), 'dd-MM-yyyy')

Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia

Postby IT2Be » Wed May 31, 2006 11:36 pm

Hi Antonio, looks great.

You triggered me by calling it a Java method. Unless I am mistaken it is JavaScript isn't it?

I also see you use the tools plugin :) Nice...

No suggestion, just wanted to give you credit for the nice work.
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby antonio » Thu Jun 01, 2006 5:29 am

Yes, Marcel, it's a JS method to use in a servoy script. Hope some people find it useful.

PS I use your tools for a lot of jobs.
Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia


Return to How To

Who is online

Users browsing this forum: No registered users and 9 guests

cron