PDA

View Full Version : Using the FormatDate() Function


Admin
August 19th, 2008, 03:17 PM
The FusionPro Date() object allows you to format the way dates appear. Below are some sample lines that show how to use the FormatDate() function:

// complete date - Ex: Thu Jan 12 2006 15:53:17 GMT-0800
return Today();

// formatted date, long - Ex: Thursday, January 12, 2006
return FormatDate(Today(), "ld, lm d, yyyy");

// formatted date, short - Ex: 01/12/06
return FormatDate(Today(), "mm/dd/yy");

Dan Korn
October 9th, 2008, 12:36 PM
This is the correct list of format specifiers in FusionPro 5.1 and later, which may differ slightly from what's listed in the FusionPro Rules System Guide:

m – month number (1-12)
mm– month number (01-12, zero-padded to two digits)
d – day number (1-31)
dd – day number (01-31, zero-padded to two digits)
yy – 2-digit year
yyyy – 4-digit year
ld – day of week name (Sunday, Monday, etc.)
sd – day of week name, abbreviated to three letter
lm – month name (January, February, etc.)
sm – month name, abbreviated to three letters
You can also use the following format specifiers for time values:

H: hours (0-23)
HH: hours (00-23, zero-padded to two digits)
h: hours (1-12)
hh: hours (01-12, zero-padded to two digits)
a: either "am" or "pm"
A: either "AM" or "PM"
n: minutes (0-59) (new in 5.8P1k)
nn: minutes (00-59, zero-padded to two digits) (new in 5.8P1k)
s: seconds (0-59)
ss: seconds (00-59, zero-padded to two digits)
So, you can do something like this:
var myDate = Today();
return FormatDate(myDate, "ld, lm d, yyyy (mm/dd/yy), h:nn:ss a");

Also new in 5.8, an optional third parameter can be used to specify a language for month and day names. Currently, this supports English (the default), German, and French. For example:

return FormatDate(Today(), "ld, lm d, yyyy", "German");

MHardee
October 31st, 2008, 06:40 AM
Another date issue to solve.. I have field "date" that I have to print it, the day before it and the day after it on the same form. Have all of that functioning.. on of the examples is:
(IN_HOME_DATE in below example contains "11/10")

date1 = Left(Field("IN_HOME_DATE"),2) + "/" + Right(Field("IN_HOME_DATE"),2) + "/" + "2008"
//return date1
var myDate= new Date(date1);
//return myDate
myDate.setDate(myDate.getDate()-1);
//return myDate
return FormatDate(myDate,"mm/dd/yyyy");

Works fine. What I have to fix is:

If the result of the date is Monday and Monday - 1 day will yield Sunday, I need to produce Saturday..

Basically:

MyDate = Monday
MyDate -1 = MyNewDate
If MyNewDate = Sunday
then MyNewDate = Saturday..

I don't want to return the text "Saturday", I need to return a true date and apply the FormatDate function, read that to see if it will be a Sunday and If so use a (-2) in the calculation instead of a (-1)...

Dan Korn
October 31st, 2008, 10:33 AM
Another date issue to solve.. I have field "date" that I have to print it, the day before it and the day after it on the same form. Have all of that functioning.. on of the examples is:
(IN_HOME_DATE in below example contains "11/10")

date1 = Left(Field("IN_HOME_DATE"),2) + "/" + Right(Field("IN_HOME_DATE"),2) + "/" + "2008"
//return date1
var myDate= new Date(date1);
//return myDate
myDate.setDate(myDate.getDate()-1);
//return myDate
return FormatDate(myDate,"mm/dd/yyyy");

Works fine. What I have to fix is:

If the result of the date is Monday and Monday - 1 day will yield Sunday, I need to produce Saturday..

Basically:

MyDate = Monday
MyDate -1 = MyNewDate
If MyNewDate = Sunday
then MyNewDate = Saturday..

I don't want to return the text "Saturday", I need to return a true date and apply the FormatDate function, read that to see if it will be a Sunday and If so use a (-2) in the calculation instead of a (-1)...

Well, this is getting bit off the topic of the FormatDate function and into general Date object handling, but you can just add some logic like this:
if (FormatDate(myDate, "ld") == "Sunday")
myDate.setDate(myDate.getDate()-1);
Or, more "JavaScript-y":
if (myDate.getDay() == 0)
myDate.setDate(myDate.getDate()-1);
For reference:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Date_Object
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Date

MHardee
November 13th, 2008, 09:47 AM
Dan (or anyone else):

Got the vast majority of this working.. I've attached all files so you can take a peek..

The part of the rule that looks at the preferred Delivery Date and adds a day and subtracts a day works fine..

I can't get it to work consistently.. Basically, all delivery days for the form are:

1 day before preferred date
the preferred date
1 day after the preferred date..

UNLESS any fall on Sunday...

Could you possibly take a peek and see where I am going wrong?

Dan Korn
November 13th, 2008, 03:28 PM
Basically, all delivery days for the form are:

1 day before preferred date
the preferred date
1 day after the preferred date..

UNLESS any fall on Sunday...

Could you possibly take a peek and see where I am going wrong?

I'm not sure I understand the requirements completely. When you say, "UNLESS any fall on Sunday," exactly what is supposed to happen if any of the dates DO fall on a Sunday? For instance, if the "preferred date" is on a Sunday, does the "middle" date (DEL_DATE) get pushed to a Saturday, or to a Monday, or what? And does that mean that the other dates are still "plus one" and "minus one" in relation to that? If the "preferred date" is on a Monday, is the "minus one" date supposed to be pushed back to Saturday, or should the entire range be pushed forward to Monday, Tuesday, and Wednesday?

Until you can provide more detailed and specific requirements, or at least a few sets of sample input and expected outputs, I'm not really sure what problem I'm trying to solve with JavaScript code.

MHardee
November 13th, 2008, 05:01 PM
I'm not sure I understand the requirements completely. When you say, "UNLESS any fall on Sunday," exactly what is supposed to happen if any of the dates DO fall on a Sunday? For instance, if the "preferred date" is on a Sunday, does the "middle" date (DEL_DATE) get pushed to a Saturday, or to a Monday, or what? And does that mean that the other dates are still "plus one" and "minus one" in relation to that? If the "preferred date" is on a Monday, is the "minus one" date supposed to be pushed back to Saturday, or should the entire range be pushed forward to Monday, Tuesday, and Wednesday?

Until you can provide more detailed and specific requirements, or at least a few sets of sample input and expected outputs, I'm not really sure what problem I'm trying to solve with JavaScript code.

Well, I did provide examples in the attached files.. They really demonstrate the problem very well..

To reiterate, NONE of the expected dates will ever fall on Sunday.. It's the date before or the date after the expected date that COULD fall on Sunday that will have to be pushed to Saturday..

Expected Day is Monday
Monday minus one Day is Sunday - that is no good
So, if expected date is Monday need to subtract TWO days
else subtract onlt one day..

With the rule I attached it works well, but, when it happens (minus two) the next record (expected day is Thursday) the calculated day returns Tuesday instead of the anticipated Monday,,,, like a counter or variable fails to reset?

I am so very close but so far....

THANKS for you help....