codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:

Home » ASP » Article

ASP Format Date and Time Script

Article by:  Jeff Anderson  ( 1362 ) (2/27/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
Viewed: 454964 times Rating (182 votes): 
 4.3 out of 5
 Rate this Article  Read Comments  Post Comments


ASP has a very handy inbuild function called FormatDateTime().

Let's start with the now() function to get the current date and time into a variable


<% 
dim todaysDate
 todaysDate=now()
 
%>


Now we can use the FormatDateTime function to format our date variable in a variety of ways.

First let's see how todaysDate appears :


<% 
Response.write todaysDate
 
%>


RESULT: 5/14/2008 5:08:23 PM

Using 0 - the vbGeneralDate format creates a date as short date (if included), and time as long time.




<% 
Response.write FormatDateTime(todaysDate,0)

 
%>


RESULT: 5/14/2008 5:08:23 PM

Using 1 - the vbLongDate shows the date as long date




<% 
Response.write FormatDateTime(todaysDate,1)

 
%>


RESULT: Wednesday, May 14, 2008


Using 2 - the vbShortDate shows the date as short date




<% 
Response.write FormatDateTime(todaysDate,2)

 
%>


RESULT: 5/14/2008


Using 3 - the vbLongTime format shows the time as long time .




<% 
Response.write FormatDateTime(todaysDate,3)

%>


RESULT: 5:08:23 PM


Using 4 - the vbShortTime format creates the current time in 24 format (hh:mm)




<%
Response.write FormatDateTime(todaysDate,4)

 
%>


RESULT: 17:08




International Date and Time

You can use the session.lcid property to change the formatting of the date and time.

For example
<%session.lcid=2057%>
will set the date and time to UK format (DD/MM/YYYY instead of MM/DD/YYYY ) Here's a list of international locales. Bear in mind that setting these will also change currency formatting.

Locale ID (LCID) Description
1033 General Unicode
33280 Binary Order
1027 Catalan
197636 Chinese Bopomofo (Taiwan)
2052 Chinese Punctuation
133124 Chinese Stroke Count
1028 Chinese Stroke Count (Taiwan)
1050 Croatian
1029 Czech
1043 Dutch
1061 Estonian
1036 French
66615 Georgian Modern
1031 German
66567 German Phone Book
1038 Hungarian
66574 Hungarian Technical
1039 Icelandic
1040 Italian
1041 Japanese
66577 Japanese Unicode
1042 Korean
66578 Korean Unicode
1062 Latvian
1063 Lithuanian
1071 FYRO Macedonian
1044 Norwegian/Danish
1045 Polish
1046 Portuguese
1048 Romanian
1051 Slovak
1060 Slovenian
1034 Spanish (Traditional)
3082 Spanish (Spain)
1053 Swedish/Finnish
1054 Thai
2057 UK English
1058 Ukrainian
1066 Vietnamese



Adding and Subtracting Dates and Times

For more information about adding and subtracting dates and times, see our ASP DateAdd article.





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'ASP Format Date and Time Script'
Posted by :  Sanjay at 13:28 on Sunday, September 21, 2003
It's great to be here to find every code u require. i wish u people all best
for the future.

It's my one source for solution : codetoad
Posted by :  Nazish at 19:03 on Wednesday, November 05, 2003
This is Great effort For creating This Code and this code enhance the ASP Techninques.
So Again this is great effort.
Posted by :  itus2006 at 21:45 on Sunday, December 28, 2003
thanks
Posted by :  dubzap at 08:37 on Saturday, March 20, 2004
Let me start with complimenting you with this great site!

I have a site where people can enter data and change them later.
One of the things that they enter is a (release)date. The format is dd-mm-yyyy (Dutch). However, somehow the server (I think it's the server...) turns it into mm-dd-yyyy. This happens only when the original day is less than 13. When i enter 13-5-2004, nothing happens. when i enter 12-5-2004 the result is 5-12-2004.

What is the solution?

thanks
Jos
Posted by :  tierneyj at 03:53 on Wednesday, July 07, 2004
I too have the issue using MS Frontpage 2000 where a user enters a date of dd/mm/yyyy and this gets posted to my MS Access database as mm/dd/yyyy, I see in your article that you mention the code '<%session.lcid=2057%>' where and how can this be used to resolve my issue? Many thanks....a tired and worn out user....
Posted by :  amoldalvi at 05:55 on Friday, July 16, 2004
Very good coding.......
Posted by :  s13khan at 11:49 on Thursday, February 10, 2005
Candle in the darkness of load-sheding...... huuh hhuuh hhaaaaah !

Very nice and simple also. Great Job. I Found very fruitful as soon as I found it......
Posted by :  elshibiny at 09:10 on Thursday, July 14, 2005
I have tried the <%session.lcid=2057%> but it didn't work. Please anyone tells me how to apply it?
Posted by :  PakXoxer at 01:05 on Monday, August 08, 2005
please those who have successfully used the code for
formating date as DD/MM/YYYY plz tell us how and where to
apply that <%sesssion.lcid=2057%> will be used.

thanks
Posted by :  ronb722 at 11:58 on Monday, October 10, 2005
Hi all,
Please forgive my ignorance, but I am new to the .NET world and am trying to format the output of my data grid. Currently it is showing date and time and I only want to see the date. How can I do this?

Thanks,
Ron
Posted by :  badrinath_subudhi at 22:13 on Monday, March 20, 2006
how to find the date format in dd-mmm-yyyy format ?
Posted by :  pranav121 at 22:24 on Thursday, February 01, 2007
<%sesssion.lcid=2057%> and conver functions

It did worked for few days and then it was showing me some error informing "Arithmatic conversion error......."
Still trying to figure out the issue.

I was using nvarchar to store date value in to datbase and than was using convert function as below
convert(datetime, columnname, 131) to convert to uk format.

of someone knows the resulution, plz advise me.
Posted by :  tenshi at 02:38 on Wednesday, April 18, 2007
First of all, thanks for posting a very helpful article Ü

Secondly, can you pls help me with this one..

We are currently supporting an old application using Win NT 4.0, Oracle 8i, MTS/IIS, and ASP.
During run-time, most of our pages has a textbox where it displays the current system date (default format is mm/dd/yyyy) on form load. Lately, there are times that it displays an invalid date format such as 04/11/197 or 04/11/07. As an immediate solution , we just restart the production server. This is an intermittent error and we cannot replicate it in our test server.

Do you have any idea why it happens?

thanks in advance Ü

Posted by :  rizwanbhutta at 03:56 on Friday, December 07, 2007
Hello
first of all thank you for comments, here are many question regarding date formating, but have no answers on this site, so i have decided to put the answers of similar questions on this site.
Question By dubzap
Answer
first of all put this statement <%session.lcid=2057%> top of your page then create a database field with settings "date/time" then try to dates and print on you page and see the result.

a similar example of this code i have put on a site where from you can check by adding removing and editing dates , the link is as
http://www.australiantalentdirectory.com/mydate
if you have any problem , you are welcome to ask at the address of webmaster@australiantalentdirectory.com
Regards
Posted by :  jn1512 at 09:55 on Thursday, March 06, 2008
I maintain a site that features Classical Music composers, many of which lived hundreds of years ago (Mozart, Beethoven, Haydn, etc.)

Has anyone encountered trouble formatting dates prior to 1750?

When I have dates from 1750 or before, the year displays properly but often the month or days are innacurate from what is in my database.

Any suggestions?


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.








Recent Forum Threads
• ** Site Hacked ** javascript inserted...
• Losing background/text color when converting HTML to PDF
• hidden div block when displayed, displays at an offset of 200px only in IE 6..
• help - sketch
• Re: Perl Script - File Handling.
• Open a file from website
• Re: to open 5 terminals from one and also execute different commands on each terminal
• read a selected multiple line, those should match some of the values read in the files..
• Help me please


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008