VB Script

I am trying to format a date to be mm/dd/yyyy

I have the script below:

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>                              
function DoDateTime(str, nNamedFormat, nLCID)                        
      dim strRet                                                
      dim nOldLCID                                                
                                                            
      strRet = str                                                
      If (nLCID > -1) Then                                          
            oldLCID = Session.LCID                                    
      End If                                                      
                                                            
      On Error Resume Next                                          
                                                            
      If (nLCID > -1) Then                                          
            Session.LCID = nLCID                                    
      End If                                                      
                                                            
      If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
      End If                                                      
                                                            
      If (nLCID > -1) Then                                          
            Session.LCID = oldLCID                                    
      End If                                                      
                                                            
      DoDateTime = strRet                                          
End Function                                                      
</SCRIPT>

If I enter the date as:  <%= DoDateTime((Comments.Fields.Item("Lastupdate").Value), 2, -1) %>

It will display: 3/24/2014

Which is great but is missing the leading zero for month and day

If I use:  <%= DoDateTime((Comments.Fields.Item("Lastupdate").Value), 2, 9225) %>

It should display the leading zeros but it doesn't in my computer :$  if I upload to another place it does show it just fine though.

Any ideas ?
LVL 1
AleksAsked:
Who is Participating?
 
englanddgConnect With a Mentor Commented:
Add this...

If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
            strRet = Right("0" & Datepart("m",strRet),2) & "/" & Right("0" & Datepart("d",strRet),2) & "/" & DatePart("yyyy",strRet)
End If
0
 
AleksAuthor Commented:
Attached is the code for this page. And two images.
Local which shows the format of the date in my development computer (WHICH IS WRONG)
And then remote in the production server in which the date shows just fine.

The include file is the exact same file as well.
code.txt
local.gif
remote.gif
0
 
Shaun KlineLead Software EngineerCommented:
How a date is displayed is dependent on the Regional settings on the machine serving the page. If you check your settings (Control Panel -> Region and Language) you will most likely see your date formatted as M/d/yyyy. The remote machine probably has this as MM/dd/yyyy.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
englanddgCommented:
@Shaun Klein is 100% correct.

However, if you want to force the output for display or export purposes, regardless of client settings, refer to my code.
0
 
AleksAuthor Commented:
In this script where do I add your code ?


--

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>                              
function DoDateTime(str, nNamedFormat, nLCID)                        
      dim strRet                                                
      dim nOldLCID                                                
                                                           
      strRet = str                                                
      If (nLCID > -1) Then                                          
            oldLCID = Session.LCID                                    
      End If                                                      
                                                           
      On Error Resume Next                                          
                                                           
      If (nLCID > -1) Then                                          
            Session.LCID = nLCID                                    
      End If                                                      
                                                           
      If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
      End If                                                      
                                                           
      If (nLCID > -1) Then                                          
            Session.LCID = oldLCID                                    
      End If                                                      
                                                           
      DoDateTime = strRet                                          
End Function                                                      
</SCRIPT>

---
0
 
englanddgCommented:
Replace line 13 - 15 with the code I posted.
0
 
AleksAuthor Commented:
By using your code I actually get:

3/24/2014 2:30:14 PM

Instead of 03/24/2014
0
 
AleksAuthor Commented:
Full code as it is now:

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>                              
function DoDateTime(str, nNamedFormat, nLCID)                        
      dim strRet                                                
      dim nOldLCID                                                
                                                            
      strRet = str                                                
      If (nLCID > -1) Then                                          
            oldLCID = Session.LCID                                    
      End If                                                      
                                                            
      On Error Resume Next                                          
                                                            
      If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
            strRet = Right("0" & Datepart("m",strRet),2) & "/" & Right("0" & Datepart("d",strRet),2) & "/" & DatePart("yyyy",strRet)
End If                                           
                                                            
      If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
      End If                                                      
                                                            
      If (nLCID > -1) Then                                          
            Session.LCID = oldLCID                                    
      End If                                                      
                                                            
      DoDateTime = strRet                                          
      

      End Function                                          
</SCRIPT>
0
 
englanddgCommented:
Ergh...sorry.  Thought that would do it, but didn't test...

I will pick this up again when I get to the office...unless someone else chimes in with a solution.

Cheers!
0
 
AleksAuthor Commented:
k
0
 
Shaun KlineLead Software EngineerCommented:
I believe the lines that you replaced were not the ones intended by englanddg.

He meant for your to replace these lines with his code:
      If ((nLCID < 0) Or (Session.LCID = nLCID)) Then                        
            strRet = FormatDateTime(str, nNamedFormat)                  
      End If

Open in new window


If that works, he gets all of the credit. If not let us know.
0
 
AleksAuthor Commented:
Problem with this is for fields with no data it displays:  0/0/

It shouldn't display anything .. for ones with data it seems to work fine
0
 
Shaun KlineConnect With a Mentor Lead Software EngineerCommented:
You can update this line:
If ((nLCID < 0) Or (Session.LCID = nLCID)) Then

Open in new window


to include a check to see if str is a valid date:

 If ((nLCID < 0) Or (Session.LCID = nLCID)) And IsDate(str) Then

Open in new window

0
 
AleksAuthor Commented:
Thanks, that worked !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.