Expand my Community achievements bar.

SOLVED

Get current date?

Avatar

Level 8

Hello

I put a DATE field in my_form, fine, but i need to pre-populate/default it with the current's date, for example, if today the user has opened the form, this field should come up with todays(11/7/2011) date.

I got the code snippets from the below links, fine

http://stackoverflow.com/questions/1531093/how-to-get-current-date-in-javascript

http://www.tizag.com/javascriptT/javascriptdate.php

http://javascript.internet.com/time-date/current-date.html

code snippet is as below,

var today = new Date();

var dd = today.getDate();

var mm = today.getMonth()+1;//January is

0!

var yyyy = today.getFullYear();

if(dd<10){dd='0'+dd}

if(mm<10){mm='0'+mm}

var today = mm+'/'+dd+'/'+yyyy; document.write(today);

It's quite complex but it will give you todays date in the format of mm/dd/yyyy.

Simply change today = mm+'/'+dd+'/'+yyyy;

document.write(today); to what ever format you wish.

But, my requirement is,

As my_form is global, hence the date should come as per user's date format, for example. for USA its MM/DD/YYYY where as for Asia and Europe its DD-MM-YYYY

1) So, pls. let me know is there ready-made / in-built function to get my requirement

2) And am thinking to write the code in field's INITIALIZATION event, is it correct? do i need to change the event to place my code?

Thank you

1 Accepted Solution

Avatar

Correct answer by
Level 10

Here is the modified Code.

if (loggedUser eq "USA")then

$ =  Num2Date(Date(), "MM/DD/YYYY"); //returns date in MM/DD/YYYY format

endif

if (loggedUser eq "Europe") then

$ = Num2Date(Date(), "DD/MM/YYYY"); //returns date in DD/MM/YYYY format

endif

Num2Date function Syntax:

Syntax

Num2Date(n [,f [, k ]])

Parameters

Parameter

Description

n

An integer representing the number of days.

If n is invalid, the function returns an error.

f (Optional)

A date format string. If you do not include a value for f, the function uses the default date format MMM D, YYYY.

k (Optional)

A locale identifier string that conforms to the locale naming standards. If you do not include a value for k, or if k is invalid, the function uses the ambient locale.

Thanks

Srini

Message was edited by: Srini Dhulipalla

View solution in original post

6 Replies

Avatar

Level 10

You can use the in-built functions available in FormCalc. You can use the Initialize event to write the script..

Num2Date(Date(), "MM/DD/YYYY") will return you the current system date in MM/DD/YYYY format.

To change it to locale specific, provide the locate value as third argument in Num2Date function. To get the more details on the locale info, open help in your Designer and search for Num2Date function.

Thanks

Srini

Avatar

Level 8

Thank you.

1) Until now, all over the my_form am using JavaScript, so is it okay now can i put FormCalc for my this requirement? or is it throws an error (for using both kind of Scripts in the same my_form)?

2) I am newbie here, pls. provide me some code snippet to achieve my requirement (for/locations are USA, Canada and Europe, we are not required for Asia at this moment)

Thank you

Avatar

Level 10

You can use FormCalc and JavaScript functions in the same form.

You have to write code in if else conditions to meet your needs.

I assume you are identifying the user location based on his/ her login to SAP system. From then on you need to pass that country info to your form.

In your initialize event you have to write if else conditions and change the date format in Num2Date function to suit your needs.

Num2Date(Date(), "MM/DD/YYYY") //returns date in MM/DD/YYYY format

Num2Date(Date(), "DD/MM/YYYY") //returns date in DD/MM/YYYY format

Hope this helps.

Thanks

Srini

Avatar

Level 8

Thank you.

For example,

my_date_field - INITIALIZATION :::::::: FormCalc

if (loggedUser == 'USA') {

var today

today = Num2Date(Date(), "MM/DD/YYYY") //returns date in MM/DD/YYYY format

this.rawValue = today " populate my_date_field with todays date

}

if (loggedUser == 'Europe) {

var today

today = Num2Date(Date(), "DD/MM/YYYY") //returns date in DD/MM/YYYY format

this.rawValue = today " populate my_date_field with todays date

}

is it OK? if not pls. correct my code

And i do not understood your stetement "To change it to locale specific, provide the locate value as third argument in Num2Date function. To get the more details on the locale info, open help in your Designer and search for Num2Date function."

Thank you

Avatar

Correct answer by
Level 10

Here is the modified Code.

if (loggedUser eq "USA")then

$ =  Num2Date(Date(), "MM/DD/YYYY"); //returns date in MM/DD/YYYY format

endif

if (loggedUser eq "Europe") then

$ = Num2Date(Date(), "DD/MM/YYYY"); //returns date in DD/MM/YYYY format

endif

Num2Date function Syntax:

Syntax

Num2Date(n [,f [, k ]])

Parameters

Parameter

Description

n

An integer representing the number of days.

If n is invalid, the function returns an error.

f (Optional)

A date format string. If you do not include a value for f, the function uses the default date format MMM D, YYYY.

k (Optional)

A locale identifier string that conforms to the locale naming standards. If you do not include a value for k, or if k is invalid, the function uses the ambient locale.

Thanks

Srini

Message was edited by: Srini Dhulipalla

Avatar

Level 8

Thank you for your reply in details, its perfect.