Solved

VB Script

Posted on 2014-03-25
14
653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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