Solved

VB Script

Posted on 2014-03-25
14
651 Views
Last Modified: 2014-03-25
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 ?
0
Comment
Question by:amucinobluedot
  • 7
  • 4
  • 3
14 Comments
 

Author Comment

by:amucinobluedot
ID: 39953231
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
 
LVL 3

Accepted Solution

by:
englanddg earned 400 total points
ID: 39953242
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
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39953249
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 3

Expert Comment

by:englanddg
ID: 39953257
@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
 

Author Comment

by:amucinobluedot
ID: 39953290
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
 
LVL 3

Expert Comment

by:englanddg
ID: 39953304
Replace line 13 - 15 with the code I posted.
0
 

Author Comment

by:amucinobluedot
ID: 39953385
By using your code I actually get:

3/24/2014 2:30:14 PM

Instead of 03/24/2014
0
 

Author Comment

by:amucinobluedot
ID: 39953386
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
 
LVL 3

Expert Comment

by:englanddg
ID: 39953404
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
 

Author Comment

by:amucinobluedot
ID: 39953411
k
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39953603
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
 

Author Comment

by:amucinobluedot
ID: 39953628
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
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 100 total points
ID: 39953764
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
 

Author Comment

by:amucinobluedot
ID: 39954019
Thanks, that worked !
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question