473,326 Members | 2,013 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,326 software developers and data experts.

Conditionally format Date/Time Values based on Time

Is there a way to conditionally format a dateTime field to produce on result
if the value is 10/31/2008 12:00 AM (user didn't enter a time) and another
result if the value is 10/31/2008 5:30 PM (user entered a time). I'm looking
for this result...

Value Result
10/31/2008 12:00 AM FRI 10/31/08
10/31/2008 12:01 AM FRI 10/31/08 12:01 AM

I also need to figure out the cell spacing to force the time onto a
different line as in

10/31/2008 12:01 AM FRI 10/31/08
12:01 AM

David H
Nov 9 '08 #1
4 3023
"dch3" <dc**@discussions.microsoft.comwrote in message
news:A5**********************************@microsof t.com...
Is there a way to conditionally format a dateTime field
A dateTime field...?

The formats you are looking for are simple enough to achieve in code - how
exactly that is done in practice will depend what you mean by a dateTime
field. Are you talking about a column in a GridView, a TextBox, a Label...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 9 '08 #2
I'm guessing you would probably do it the same way as any other conditional
statement, where the conditional determines the format string to use. What
you will use as the condition may be a little tricky, since it is obviously
quite possible for the user to enter a time of 12:00 AM, so looking at the
time entered would not be enough to determine whether a time was entered.
How you will apply the format string will depend on where you are displaying
the date and possibly a time. If it is simply a TextBox or Label or some
other control with just a simple Text property, you can just use the
DateTime.ToString(formatstring) method. If you are using a databound control
such as a Repeater or DataList, you may need to do it in the ItemDataBound
event. I'm not going to get into details of what you should do, since I
haven't seen your code, but hopefully this can get you started. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"dch3" <dc**@discussions.microsoft.comwrote in message
news:A5**********************************@microsof t.com...
Is there a way to conditionally format a dateTime field to produce on
result
if the value is 10/31/2008 12:00 AM (user didn't enter a time) and another
result if the value is 10/31/2008 5:30 PM (user entered a time). I'm
looking
for this result...

Value Result
10/31/2008 12:00 AM FRI 10/31/08
10/31/2008 12:01 AM FRI 10/31/08 12:01 AM

I also need to figure out the cell spacing to force the time onto a
different line as in

10/31/2008 12:01 AM FRI 10/31/08
12:01 AM

David H

Nov 9 '08 #3
A field from a database displayed in a GridView. My experience is with
Classic ASP. I just started playing with ASP.NET tonight. (Damn I wish I had
tried this sooner)

"Mark Rae [MVP]" wrote:
"dch3" <dc**@discussions.microsoft.comwrote in message
news:A5**********************************@microsof t.com...
Is there a way to conditionally format a dateTime field

A dateTime field...?

The formats you are looking for are simple enough to achieve in code - how
exactly that is done in practice will depend what you mean by a dateTime
field. Are you talking about a column in a GridView, a TextBox, a Label...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 9 '08 #4
"dch3" <dc**@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
>>Is there a way to conditionally format a dateTime field

A dateTime field...?

A field from a database displayed in a GridView.
Ah...
My experience is with Classic ASP.
The best advice I can give you is to forget everything you ever knew about
ASP Classic and learn ASP.NET properly from the ground up. The biggest
mistake people make when coming from ASP Classic to ASP.NET is to assume
that ASP.NET is somehow the "next version" of ASP Classic. It isn't - not
even close. ASP.NET is different from ASP Classic in almost every way,
mainly due to the .NET Framework.

It's a steep learning curve. I'd suggest you get a copy of these:
http://www.amazon.com/Beginning-ASP-...6236712&sr=8-2
http://www.amazon.com/Pro-ASP-NET-20...6236712&sr=8-1
and work your way through them.

Anyway, to answer your question, you'll need to wire up the GridView's
OnRowDataBound event:
http://www.google.co.uk/search?sourc...OnRowDataBound

This will allow you to modify the values of a row's cells as they are being
bound to the GridView. E.g. if the date value was contained in the third
column of the GridView, you might do something like this:

<asp:GridView ID="gvResults" runat="server"
OnRowDataBound="gvResults_RowDataBound">

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dtmCell = DateTime.Parse(e.Row.Cells[2].Text);
e,Row.Cells[2].Text = dtmCell.ToString("ddd MM/dd/yy").ToUpper();
if (dtmCell.Hour + dtmCell.Minute + & dtmCell.Second 0)
{
e.Row.Cells[2].Text += "<br />" + dtm.Cell.ToString("hh:mm tt");
}
}
}

Also, be aware that the date format FRI 10/31/08 is not Y2K-compliant
because it doesn't display a 4-digit year, and is also internationally
ambiguous. E.g. in that format, today's date would be

"SUN 11/09/08"

In the vast majority of the world, that would be interpreted as 11th
September 2008, not 9th November 2008 and, depending on what it was used
for, would fail validation because 11th September 2008 was a Tuesday, not a
Sunday. In some parts of the world, that would be interpreted as 8th
September 2011.

If your web app is intended to be used by anyone outside the US and some
parts of Canada, Kenya and the Philippines, you should not use this date
format:
http://en.wikipedia.org/wiki/Image:Date.png
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
2
by: Tatiana Zadiraka | last post by:
I use DB2 8.1 with FixPack 5. In command line for sql I get all DATE columns only in MM-DD-YYYY format. Of course, DATE('20-12-2003') gives me an error SQL0181N The string representation of a...
3
by: mark | last post by:
How do I get all fields on one page of a report? I have a report that has a column for each day of the week and 6 records for each day. I need each weekday's records returned on only one detail...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
1
by: balleyman47 | last post by:
getting the following error when executing an insert statement: java.lang.IllegalArgumentException: Date/Time must be JDBC format running on UDB 8.2 fix pack 10 using IBM type IV driver. Query...
10
by: ARC | last post by:
Hello all, General question for back-end database that has numerous date fields where the database will be used in regions that put the month first, and regions that do not. Should I save a...
18
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st...
2
by: shannonwhitty | last post by:
I am able to extract dates in the correct format i.e. SELECT CONVERT(VARCHAR(8), GETDATE(), 3) =dd/mm/yy My issue is that my users are selecting a date in this format and I need to select...
3
by: Naushad | last post by:
Hi All, I am trying to open the report conditionally from the dialogue box form. In the form there are three field. cboEmployees StartDate EndDate I have used these field in criteria to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.