Solved

VB Script

Posted on 2014-03-25
14
647 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now