cancel
Showing results for 
Search instead for 
Did you mean: 

Trasnform current date to format MM/dd/yyyy HH:mm:ss AM/PM

Former Member
0 Kudos

Hi All,

How do I transform current date to format MM/dd/yyyy HH:mm:ss AM/PM?

I am able to get the format MM/dd/yyyy HH:mm:ss by using the standard date function. How do I get AM or PM to end of MM/dd/yyyy HH:mm:ss??

Thanx

Navin

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Navin,

<u><b>you do not need any UDF for this</b></u>

you can do it using Current Date and specifing <b>hh:mm a</b> as the format.

You can use this format as well with DateTrans block if you need to translate a date and not to get a timestamp.

Have a look at this java doc, it could help: <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html">Class SimpleDateFormat</a>

Hope this solve your problem

Kind Regards,

Sergio

Former Member
0 Kudos

Hi Sergio,

Thats great. I used target format as MM/dd/yyyy HH:mm a in std function Current Date. I got the format in 02/07/2007 11:24 AM. Can I get Seconds also???

Hi Venkat,

Thanks for the sample code. Let me try that code.

Statish Reddy,

Thanks

Awarded points accordingly.

Thanx Guys

Navin

Former Member
0 Kudos

Navin,

using the same class as Sergio said u can get Second

yyyy.MM.dd G 'at' HH:mm:ss -- > 2001.07.04 AD at 12:08:56

Regards

Kavitha

Former Member
0 Kudos

Hi Navin,

for sure, you can customize the output as you need.

In this case you need to specify the string <b>MM/dd/yyyy HH:mm:ss a</b>.

You can go through <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html">Class SimpleDateFormat</a> to explore all the possible output format.

Kind Regards,

Sergio

Former Member
0 Kudos

Thanks Sergio. I have another probelm with mapping. I have posted it in the following thread:

"Mapping help to create node for each souce field ".

Can you please take a look and see if you can help me.

Thanx

Navin

Former Member
0 Kudos

Sure, I will have a look!

PS: don't forget to mark the problem as solved if so!

Regards,

Sergio

Former Member
0 Kudos

Thanks. Closing the thread.

Navin

Former Member
0 Kudos

Hi Navin,

I think you cannot achieve it using standard functions. You need to go for an User Defined Function.

Use an UDF after the DateTrans function. In that UDF you can append AM or PM to it. I think you need to add logic to find whether to append AM or PM. If you are unable to find the logic, post some sample values so that we can help you with the code.

Regards,

P.Venkat

Former Member
0 Kudos

Hi Venkat,

Thanks for quick reply. It would be great help if you could provide me the code for UDF. Lets take following example: (this is the format after using std function Date)

02/07/2007 11:38:40

02/07/2007 16:38:40

Thanx

Navin

Former Member
0 Kudos

Hi Navin,

How AM/PM is added? In the sample values given its in 24 hrs notation. Are u sure you want to add AM/PM to this notation? or U want to change this 24 hrs notation into 12 hrs and add AM/PM?

For converting into 12 hrs and appending AM/PM you can use this code:

input a:



int hr = Integer.parseInt(a.substring(11,13));
String appendVal = "AM";

if(hr > 12){
   hr = hr - 12;
   appendVal = "PM";
} else if(hr == 12){
   appendVal = "PM";
} else if(hr == 00){
   hr = hr + 12;
}

Sting finalVal = a.substring(0,11) + String.ValueOf(hr) + a.substring(13,a.length()) + appendVal;

return finalVal;

Regards,

P.Venkat

Message was edited by:

Venkataramanan

Former Member
0 Kudos

Navin,

I dont think your requirement is clear. But as far as I understood this is what you are looking for:

Currentdate --- Substring(12,13) --> Compare with Greater and constant 12 --> Then give this to IF condition. If this is true then Current Date + Concat + Constant PM else Current Date + Concat + Constant AM. It will work.

But only thing I am concerned here is with time. If your time is 14:59:21 then I think it should show as 02:59:21PM but not 14:59:21PM. My above logic works for this only. So please go through your requirement once again.

---Satish

ranjit_deshmukh
Active Participant
0 Kudos

Hi Navin,

the solution provided by Satish is fine

but if you want it as 02:59:21 PM inspite of 14:59:21 PM

in that case UDF is the only option useful for you.

in that just check the 'hh' part if it is more than 12

just subtract 12 from it.

Hope you got it.

Ranjit.

Former Member
0 Kudos

Hi Venkat,

My bad....you are right, it is 12hr notation only.

Thanx

Navin

Former Member
0 Kudos

Hi,

Try the code given in my previous post. I think that can solve the issue.

Regards,

P.Venkat

Former Member
0 Kudos

u can do it by using datetransformation function in XI. u can use it while doing graphical mapping