473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how i convert date into specific format in asp

how i convert date into specific format in asp?

like in date format 'Jun 17, 2007' ?
Jun 13 '07 #1
1 12839
how i convert date into specific format in asp?

like in date format 'Jun 17, 2007' ?

I use this website for alot of help. www.learnasp.com

here's some information about the formatdate options. Not sure what your wanting and what your starting with so this might cover it for you.

Date and Time By Tony Arguelles tony@southbaywebdesigns.com
Summary
Including the date and/or time on a web page can be a subtle yet valuable addition when designing a web site. The addition of the date to the home page can create the impression that a site is constantly being updated with new content since each time a visitor loads the page, the current date will be displayed.

In this tutorial I'll teach you how to add the date and time to your ASP pages using the VBScript FormatDateTime() function. I'll explain how the function works, teach you how to integrate it into your ASP pages and illustrate the output you'll get depending on the arguments you pass. I'll round out the tutorial of the FormatDateTime() function by covering a few limitations that it has, which might or might not be a big deal depending on your specific needs.

Just FYI, this article assumes you know basic HTML and how to add ASP scripts to your web pages.

The FormatDateTime() Function
Microsoft provides a ton of predefined VBScript functions designed to reduce coding time. The FormatDateTime() function is one of those powerful functions and is really easy to use, too. This function uses the following format:

FormatDateTime(date, format)

There are two arguments the function accepts: date and format. Table 1-1 below describes these arguments in greater detail:

Table 1-1: The FormatDateTime() function and its arguments Argument Argument Description
date This argument is required and can be any valid date expression such as Date or Now


format This format constant or format value specifies how the date and/or time will be displayed on your ASP page.

When specifying the format argument, you can either type the Visual Basic constant name (name in left column), or the constant's corresponding value (0 - 4, from the middle column). They do the same thing, it's just less typing if you use the value.


Constant Format Value Format Description
vbGeneralDate 0 This is the default. Not specifying a value or specifying 0 will produce a date in the format of mm/dd/yy.

If the date expression is Now, it will also return the time, after the date, in hh:mm:ss PM/AM format.


vbLongDate 1 This is my personal favorite :-) Passing this value will produce a date in the format of
weekday, month day, year*

* The year is Y2K compliant :-).



vbShortDate 2 Passing this value returns a date formatted just like the default of 0 (mm/dd/yy).


vbLongTime 3 Passing this value returns the time in hh:mm:ss PM/AM format.


vbShortTime 4 Passing this value returns military time in this format hh:mm



Table 1-1 is a good reference once you've got a feel for how the FormatDateTime() function works or if you're an experienced programmer. For those of you that aren't clear on how all the information in table 1-1 relates to "real world" implementations, let's take a look at some examples:



Returning the Current Date
If you would like to display the current date, here are a few different ways to do it along with the results they produce:

<%= FormatDateTime(Date) %> returns:
(You would get the same result by coding this: <%= FormatDateTime(Date, 0) %>)

<%= FormatDateTime(Date, 1) %> returns:

<%= FormatDateTime(Date, 2)%> returns:


Returning the Current Time
If you would like to return the current time, here are a couple of ways to do that:

<%= FormatDateTime(Now, 3)%> returns: 8:35:32 AM

<%= FormatDateTime(Now, 4)%> returns: 08:35

Returning the Current Date and Time
If you would like to return the current date and time together, here's how to do just that:

<%= FormatDateTime(Now) %> returns: 6/13/2007 8:35:32 AM

If you're like me, you probably don't like the way the date and time displays above; it's not very cool looking, is it? In cases like this, you can actually include two FormatDateTime() functions next to each other, in order to get the date and time in a more desriptive format, like this:

<%= FormatDateTime(Date, 1) %>&nbsp;&nbsp;<%= FormatDateTime(Now, 3)%> returns:
8:35:32 AM

Adding the code to your page
Integrating the code into your ASP pages is really easy; here is how the code would look on a page with basic HTML to display the date:

<html>
<head>
<title>Here's the date</title>
</head>

<body>


Thank you for coming to this page. The current date is: <%= FormatDateTime(Date, 1) %>


</body>


</html>

Limitations of the FormatDateTime() function
The FormatDateTime() function is an extremely handy bit of code that can help you add a touch of flair almost instantly. I would like to mention four limitations that stick out in my mind, which may be an issue to you (or your clients) depending on the project at hand:

Limitation 1: The unneeded zero
On the first through ninth days of a month the day shows up in the format of "Month 01, Year". I know it seems like a small thing but trust me, it's can be a big deal to some.

Limitation 2: Only basic formatting allowed
You are limited to basic formatting of the string that's returned by the FormatDateTime() function. Since the date and/or time function returns is a single string, you can bold, italicize and change the whole date/time by adding HTML or style sheet tags around it, but you can't change the display properties for a single part (e.g., the month).

With the FormatDateTime() function, you can do this:

<b><%= FormatDateTime(Date, 1) %></b> which would return this:

But you can't do this:

Wednesday, January 1, 1999

If limitations 1 or 2 are a major hang up for you, you'll need to use different ASP/VBScript techniques to add the date to your page. I'll cover those in my next article!

Limitation 3: The time isn't necessarily "their" time.
Limitation 3 is more of a by-product than a limitation, but I figured I would keep the naming conventions the same for this section. If you use the the FormatDateTime() function (or any other date related function) on the server side, the date/time returned will be whatever the server's date and time is, not your client's time from their system.

If you want to ensure that the date and/or time a visitor sees on your page is the date in their part of the coutry or world, then consider using client side VBScript as an Internet Explorer only solution, or switch to client side JavaScript for a universal browser solution.

Limitation 4: It's static.
Think of the displayed date or time as a "snap shot" of when the page was requested by the visitor. You cant use this function to display a "clock" that updates every second, or automatically update the date on the page when one day turns to the next.

If you wanted to display a dynamic clock on your page, you would need to use client side JavaScript, or VBScript (IE only) to handle that task.

I hope you've enjoyed this article on the FormatDateTime() function. I'll be back soon with more date and time related ASP fun! If you have any questions or comments, send me an Email at: tony@southbaywebdesigns.com
Jun 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ohaya | last post by:
Hi, I'm working with a date string with a format as follows: Sat Dec 25 21:32:59 EST 2004 and I want to compare it to another date string of the format: 7/28/2004
2
by: Annie D via AccessMonster.com | last post by:
Hi, I am using the following SQL statement courtesy of access monster user: 'Terry Kreft ' to convert dates from a text format (20020331 00:00:00) to a recognised date/time format (31/03/2002)....
1
by: seash | last post by:
H when i execute this query, my query is crashing throwing an exception "cant convert char datatype to datetime type format my query is "insert into mytable(mydate) values ('"+ varmydate+ "')...
15
by: JenHu | last post by:
Hi expert, I have a data field (birthdate) contains a datetime datatype (mm/dd/yyyy), how can I convert to mmdd?? say, the birthday is 8/1/1979 and convert to mmdd DOB =...
1
by: prabhunew2005 | last post by:
I am using postgres,php in web designing. I have to convert date entered in MM-DD-YYYY format to YYYY-MM-DD format. I have to use resulting date format in my search sql. I used todate method. But i...
9
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i...
5
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
4
by: Tony B | last post by:
I'm looking to convert a string in "dd/MM/yy" format into a date object. How do I go about this ?
3
by: ssmeshack | last post by:
Hi all, Im have a problem here. Im using VWD 2008 (c#) and Mysql 2005. I dont know how to convert datetime data that i retrieve from mysql database to only date or time. Can anyone help me? Here...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.