By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

ASP.Net [2.0] - Converting a date to a new format...

P: n/a
Hi all,

Ok - I've come from a 1.1 background - and previously I've never had any
problem with doing this:

Response.Write (Session("MyDate").ToString("dd/MM/yyyy"))

So, I might get this for example:

21/05/2006

Ok - just tried exactly the same thing in .net 2.0 and I'm having no end of
problems..

My date comes from my SQL Server database which is stored as a datetime.

This value gets plonked into my class which both stores the value as a DATE
and returns the value via a property as a DATE.

It doesn't seem to matter what I try I can either only dump out the whole
value (21/05/2006 19:59:00) or get myself a nice error message...

To say that this was beginning to give me the "red arse" would be an
understatement...I therefore admit defeat and ask that someone wiser in this
group assist me..

I thank you in advance my .Net Grand-Masta's...

Rob :o)
May 21 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Rob Meade" <te*********************@edaem.bbor>'s wild
thoughts were released on Sun, 21 May 2006 19:00:46 GMT
bearing the following fruit:
Hi all,

Ok - I've come from a 1.1 background - and previously I've never had any
problem with doing this:

Response.Write (Session("MyDate").ToString("dd/MM/yyyy"))

So, I might get this for example:

21/05/2006

Ok - just tried exactly the same thing in .net 2.0 and I'm having no end of
problems..

My date comes from my SQL Server database which is stored as a datetime.

This value gets plonked into my class which both stores the value as a DATE
and returns the value via a property as a DATE.

It doesn't seem to matter what I try I can either only dump out the whole
value (21/05/2006 19:59:00) or get myself a nice error message...

To say that this was beginning to give me the "red arse" would be an
understatement...I therefore admit defeat and ask that someone wiser in this
group assist me..

I thank you in advance my .Net Grand-Masta's...

Rob :o)


I'm not clear what the problem is, you appear to be saying
you want date variables to act like formatted strings and
that ain't gonna happen.

What does it matter how the data variable holds the date?
Aren't you displaying the date in the format you desire
anyway?

Jan Hyde (VB MVP)

--
What's the difference between a tale of lost cash and a secluded
religious community?
One is a money story, and the other is a monastery

(Lars Hanson)

May 22 '06 #2

P: n/a
"Jan Hyde" wrote ...
I'm not clear what the problem is, you appear to be saying
you want date variables to act like formatted strings and
that ain't gonna happen.
Hi Jan, thank you for your reply.

Clearly I didn't explain myself very well...my apologies.
What does it matter how the data variable holds the date?
Aren't you displaying the date in the format you desire anyway?


I have the date stored in the database and my class as a date, it also has
the time etc...

What I want to be able to do on my web page is say this:

"Last updated: 22/05/2006 11:46"

what I dont want is this:

"Last updated: 05/22/2006 11:46:01 AM" or whatever else it feel like adding
etc :)

What I used to be able to do was this:

Response.Write (myDate.ToString("dd/MM/yyyy HH:mm")

and I get exactly that:
22/05/2006 11:46

I am unable to do this now in .net 2.0 - the ToString function does not give
you the format stuff in the brackets, instead I get an "index" instead?
whats a bit weird is that I can type..

..ToString.ToString("dd/MM/yyyy") - not that it worked, but I thought it odd
I could tostring a tostring??!

Is any of this any clearer?

Regards

Rob

PS: What do you use to configure that reply at the top?
ie,
May 22 '06 #3

P: n/a
Damn that control+enter keyboard short cut!!!

What I meant was - how do you automate this kind stuff with your own
message - I've seen it before but never been able to find a way of doing
it..
"Rob Meade" wild thoughts were released on Sun, 21 May 2006 19:00:46 GMT
bearing the following fruit:


hehe
May 22 '06 #4

P: n/a
"Rob Meade" <ku***************@edaem.bor>'s wild thoughts
were released on Mon, 22 May 2006 11:49:06 +0100 bearing the
following fruit:
"Jan Hyde" wrote ...
I'm not clear what the problem is, you appear to be saying
you want date variables to act like formatted strings and
that ain't gonna happen.
Hi Jan, thank you for your reply.

Clearly I didn't explain myself very well...my apologies.
What does it matter how the data variable holds the date?
Aren't you displaying the date in the format you desire anyway?


I have the date stored in the database and my class as a date, it also has
the time etc...

What I want to be able to do on my web page is say this:

"Last updated: 22/05/2006 11:46"

what I dont want is this:

"Last updated: 05/22/2006 11:46:01 AM" or whatever else it feel like adding
etc :)

What I used to be able to do was this:

Response.Write (myDate.ToString("dd/MM/yyyy HH:mm")

and I get exactly that:
22/05/2006 11:46

I am unable to do this now in .net 2.0 - the ToString function does not give
you the format stuff in the brackets, instead I get an "index" instead?
whats a bit weird is that I can type..

OK, can you show us how 'mydate' is declared because I don't
see "index"
.ToString.ToString("dd/MM/yyyy") - not that it worked, but I thought it odd
I could tostring a tostring??!

Is any of this any clearer?

Regards

Rob

PS: What do you use to configure that reply at the top?
ie,


Depends on your newsreader, you should be able to configure
the that text.


Jan Hyde (VB MVP)

--
Sinuses run in our family (Richard Lederer)

May 22 '06 #5

P: n/a
"Rob Meade" <ku***************@edaem.bor>'s wild thoughts
were released on Mon, 22 May 2006 11:55:25 +0100 bearing the
following fruit:
Damn that control+enter keyboard short cut!!!

What I meant was - how do you automate this kind stuff with your own
message - I've seen it before but never been able to find a way of doing
it..
"Rob Meade" wild thoughts were released on Sun, 21 May 2006 19:00:46 GMT
bearing the following fruit:


hehe


I tried outlook express a couple of times but I find it so
difficult to use as a newsreader I give up. Give Free Agent
a try, I find it very easy to use.

Jan Hyde (VB MVP)

--
Sinuses run in our family (Richard Lederer)

May 22 '06 #6

P: n/a
"Jan Hyde" wrote ...
I tried outlook express a couple of times but I find it so
difficult to use as a newsreader I give up. Give Free Agent
a try, I find it very easy to use.


Hi Jan,

Thanks for that - I'll have a looky this evening - appreciated :o)

Rob
May 22 '06 #7

P: n/a
"Jan Hyde" wrote ...
OK, can you show us how 'mydate' is declared because I don't see "index"


okey dokey - I'll post it up this evening, its on my laptop at home at the
moment....if its of any help - I can roughly remember (but will still post
this evening).,..

My "User" class has a class variable

Private _profileLastUpdated AS Date

I also have a property for that..

Public Property ProfileLastUpdated As Date
Get
...
Set
..
End Property

I populate this via my data access layer....

Where I have a function that populates a datatable, a item is taken from the
row and pushed into the property of my User class...

Something like this:

User.ProfileLastUpdated = DataTable.Rows(0).Item("ProfileLastUpdated")

I think thats about it - as I said I'll post the code up propertly this
evening, but that all feels familiar to me....

On the web page I then have a label which I populate on my page load (or at
least a function called from the page load)....

lblProfileLastUpdated = "Last updated: " & User.ProfileLastUpdated

that would give me...

Last Updated: 22/05/2006 15:27:00

I basically want to lose the seconds from that - and a character mangling
solution wasn't really what I wanted (ie, using LEFT / RIGHT / LEN) -as I'd
used the format part of .ToString before I assumed it would work fine here
too...

Regards

Rob
May 22 '06 #8

P: n/a
"Rob Meade" <ku***************@edaem.bor>'s wild thoughts
were released on Mon, 22 May 2006 15:28:31 +0100 bearing the
following fruit:
"Jan Hyde" wrote ...
OK, can you show us how 'mydate' is declared because I don't see "index"


okey dokey - I'll post it up this evening, its on my laptop at home at the
moment....if its of any help - I can roughly remember (but will still post
this evening).,..

My "User" class has a class variable

Private _profileLastUpdated AS Date

I also have a property for that..

Public Property ProfileLastUpdated As Date
Get
...
Set
..
End Property

I populate this via my data access layer....

Where I have a function that populates a datatable, a item is taken from the
row and pushed into the property of my User class...

Something like this:

User.ProfileLastUpdated = DataTable.Rows(0).Item("ProfileLastUpdated")

I think thats about it - as I said I'll post the code up propertly this
evening, but that all feels familiar to me....

On the web page I then have a label which I populate on my page load (or at
least a function called from the page load)....

lblProfileLastUpdated = "Last updated: " & User.ProfileLastUpdated

that would give me...

Last Updated: 22/05/2006 15:27:00

I basically want to lose the seconds from that - and a character mangling
solution wasn't really what I wanted (ie, using LEFT / RIGHT / LEN) -as I'd
used the format part of .ToString before I assumed it would work fine here
too...

Regards

Rob


I tried to mimic your setup but I still get the option to
provide a format string. I just know this is gonna be
something so simple we'll both kick ourselves.


Jan Hyde (VB MVP)

--
Metallurgist: Someone who is allergic to iron. (Leo Roston)

May 22 '06 #9

P: n/a
"Jan Hyde" wrote ...
I tried to mimic your setup but I still get the option to
provide a format string. I just know this is gonna be
something so simple we'll both kick ourselves.


Hi Jan, ok - my apologies for the delay - the following is my code, its a
bit lengthly for a group posting but I figured you'd wanna see it all
really..please also note that this is a work in progress so some things
aren't finished yet (although feel free to comment upon anything you thinks
a bit poor)...you might wanna paste this into VS to make it reformat a bit
better...

Basically, when I log in, I run a function to get my the user (GetUser) -
which I pass my username/password to (I mention this only because there are
2 GetUser functions below).

This returns me my "User" object - the ProfileLastUpdated has been populated
with a date from SQL Server and it includes the time.

I then populate my session variables accordingly; for the one in question:

Session("ProfileLastUpdated") = User.ProfileLastUpdated

This should currently hold a "date"

On my "UpdateProfile.aspx" page, if the page is not a post back then I
display a label which should have a date and time in it:

lblProfileLastUpdated.Text = "last updated " & <my_value_goes_here>

So, if I do this:
lblProfileLastUpdated.Text = "last updated " & Session("ProfileLastUpdated")

I get this on the page:

"last updated 23/05/2006 19:17:21" for example...

Now - the objective of all of this was to make it a bit more friendly,
initially I wanted this format "dd/MM/yyyy HH:mm" - so just losing the
seconds - so I tried this:

lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString(" <-- the intellisense at this point
displays and I get a the following in the box

Chars (index As Integer) As Char
index:
A character position in this instance.

Now - here's where it gets even more fruity....

If I type this:
lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString().ToString( <-- the intellisense at this
point displays and I get two options...

I can have .ToString() which says it just returns the instance of a string
and no conversion is made, or
I can have .ToString(provider as System.IFormatProvider)
"Provider: (reserved) An System.IFormatProvider that provides culture
specific formating information."

I hope all of this makes some sense to you - as at no point does it seem
happy to just do this:

lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString("dd/MM/yyyy HH:mm")

as I would have liked and expected...
User.vb

Imports Microsoft.VisualBasic
Public Class User
' class variables
Private _userID As Integer
Private _userForename As String
Private _userSurname As String
Private _userEmailAddress As String
Private _userPermissionID As Integer
Private _userPermissionName As String
Private _userUsername As String
Private _userProfileLastUpdated As Date
' instantiate
Public Sub New()
End Sub
' properties
Public Property ID() As Integer
Get
Return _userID
End Get
Set(ByVal value As Integer)
_userID = value
End Set
End Property
Public Property Forename() As String
Get
Return _userForename
End Get
Set(ByVal value As String)
_userForename = value
End Set
End Property
Public Property Surname() As String
Get
Return _userSurname
End Get
Set(ByVal value As String)
_userSurname = value
End Set
End Property
Public ReadOnly Property FullName() As String
Get
Return _userForename & " " & _userSurname
End Get
End Property
Public Property EmailAddress() As String
Get
Return _userEmailAddress
End Get
Set(ByVal value As String)
_userEmailAddress = value
End Set
End Property
Public Property PermissionID() As Integer
Get
Return _userPermissionID
End Get
Set(ByVal value As Integer)
_userPermissionID = value
End Set
End Property
Public Property PermissionName() As String
Get
Return _userPermissionName
End Get
Set(ByVal value As String)
_userPermissionName = value
End Set
End Property
Public Property Username() As String
Get
Return _userUsername
End Get
Set(ByVal value As String)
_userUsername = value
End Set
End Property
Public Property ProfileLastUpdated() As Date
Get
Return _userProfileLastUpdated
End Get
Set(ByVal value As Date)
_userProfileLastUpdated = value
End Set
End Property
' methods
End Class

DataAccess.vb (note I've stripped out all the methods that were not related
to the poplation of the User.vb class)

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Public Class DataAccess
' class variables
Private _homeMovieDatabaseConnectionString As String
' instantiate
Public Sub New(ByVal connectionString As String)
_homeMovieDatabaseConnectionString = connectionString
End Sub
' methods

Public Function GetUser(ByVal username As String, ByVal password As String)
As User
' declare variables
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Parameters As ArrayList
Dim DataAdapter As SqlDataAdapter
Dim DataTable As DataTable
Dim User As User
' create new instances of our objects
User = New User
Parameters = New ArrayList
' add parameter to parameters
Parameters.Add(New SqlParameter("@Username", username))
Parameters.Add(New SqlParameter("@Password", password))
' create new instances of our objects
Connection = GetConnection()
Command = GetCommand(Connection, "GetUserByUsernameAndPassword",
CommandType.StoredProcedure, Parameters)
DataAdapter = GetDataAdapter(Command)
DataTable = GetDataTable()
' execute command and fill data adapter
DataAdapter.Fill(DataTable)
' check to see if we returned a row
If DataTable.Rows.Count > 0 Then
' populate
User.ID = DataTable.Rows(0).Item("UserID")
User.Forename = DataTable.Rows(0).Item("UserForename")
User.Surname = DataTable.Rows(0).Item("UserSurname")
User.EmailAddress = DataTable.Rows(0).Item("UserEmailAddress")
User.ProfileLastUpdated = DataTable.Rows(0).Item("UserProfileLastUpdated")
User.PermissionID = DataTable.Rows(0).Item("PermissionID")
User.PermissionName = DataTable.Rows(0).Item("PermissionName")
User.Username = DataTable.Rows(0).Item("LogonUsername")
Else
' clear object
User = Nothing
End If
' tidy up
Parameters = Nothing
Command.Dispose()
Command = Nothing
Connection.Close()
Connection = Nothing
' return result
Return User
End Function

Public Function GetUser(ByVal userID As Integer) As User
' declare variables
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Parameters As ArrayList
Dim DataAdapter As SqlDataAdapter
Dim DataTable As DataTable
Dim User As User
' create new instances of our objects
User = New User
Parameters = New ArrayList
' add parameter to parameters
Parameters.Add(New SqlParameter("@UserID", userID))
' create new instances of our objects
Connection = GetConnection()
Command = GetCommand(Connection, "GetUserByUserID",
CommandType.StoredProcedure, Parameters)
DataAdapter = GetDataAdapter(Command)
DataTable = GetDataTable()
' execute command and fill data adapter
DataAdapter.Fill(DataTable)
' check to see if we returned a row
If DataTable.Rows.Count > 0 Then
' populate
User.ID = DataTable.Rows(0).Item("UserID")
User.Forename = DataTable.Rows(0).Item("UserForename")
User.Surname = DataTable.Rows(0).Item("UserSurname")
User.EmailAddress = DataTable.Rows(0).Item("UserEmailAddress")
User.ProfileLastUpdated = DataTable.Rows(0).Item("UserProfileLastUpdated")
User.PermissionID = DataTable.Rows(0).Item("PermissionID")
User.PermissionName = DataTable.Rows(0).Item("PermissionName")
User.Username = DataTable.Rows(0).Item("LogonUsername")
Else
' clear object
User = Nothing
End If
' tidy up
Parameters = Nothing
Command.Dispose()
Command = Nothing
Connection.Close()
Connection = Nothing
' return result
Return User
End Function
Private Function GetConnection() As SqlConnection
' declare variables
Dim Connection As SqlConnection
' create a new instance of our object
Connection = New SqlConnection(_homeMovieDatabaseConnectionString)
' open connection
Connection.Open()
' return connection
Return Connection
End Function
Private Function GetCommand(ByVal connection As SqlConnection, ByVal
commandText As String, ByVal commandType As System.Data.CommandType) As
SqlCommand
' declare variables
Dim Command As SqlCommand
' create a new instance of our object
Command = New SqlCommand
' define command properties
Command.Connection = connection
Command.CommandText = commandText
Command.CommandType = commandType
' return command
Return Command
End Function
Private Function GetCommand(ByVal connection As SqlConnection, ByVal
commandText As String, ByVal commandType As System.Data.CommandType, ByVal
parameters As ArrayList) As SqlCommand
' declare variables
Dim Command As SqlCommand
Dim Parameter As SqlParameter
' create a new instance of our object
Command = New SqlCommand
' define command properties
Command.Connection = connection
Command.CommandText = commandText
Command.CommandType = commandType
' add parameters
For Each Parameter In parameters
Command.Parameters.Add(Parameter)
Next
' return command
Return Command
End Function
Private Function GetDataAdapter(ByVal command As SqlCommand) As
SqlDataAdapter
' declare variables
Dim DataAdapter As SqlDataAdapter
' create a new instance of our object
DataAdapter = New SqlDataAdapter(command)
' return data adapter
Return DataAdapter
End Function
Private Function GetDataTable() As DataTable
' declare variables
Dim DataTable As DataTable
' create a new instance of our object
DataTable = New DataTable
' return data table
Return DataTable
End Function
End Class
May 23 '06 #10

P: n/a
"Rob Meade" <te*********************@edaem.bbor>'s wild
thoughts were released on Tue, 23 May 2006 18:31:39 GMT
bearing the following fruit:
"Jan Hyde" wrote ...
I tried to mimic your setup but I still get the option to
provide a format string. I just know this is gonna be
something so simple we'll both kick ourselves.


Hi Jan, ok - my apologies for the delay - the following is my code, its a
bit lengthly for a group posting but I figured you'd wanna see it all
really..please also note that this is a work in progress so some things
aren't finished yet (although feel free to comment upon anything you thinks
a bit poor)...you might wanna paste this into VS to make it reformat a bit
better...

Basically, when I log in, I run a function to get my the user (GetUser) -
which I pass my username/password to (I mention this only because there are
2 GetUser functions below).

This returns me my "User" object - the ProfileLastUpdated has been populated
with a date from SQL Server and it includes the time.

I then populate my session variables accordingly; for the one in question:

Session("ProfileLastUpdated") = User.ProfileLastUpdated

This should currently hold a "date"

On my "UpdateProfile.aspx" page, if the page is not a post back then I
display a label which should have a date and time in it:

lblProfileLastUpdated.Text = "last updated " & <my_value_goes_here>

So, if I do this:
lblProfileLastUpdated.Text = "last updated " & Session("ProfileLastUpdated")

I get this on the page:

"last updated 23/05/2006 19:17:21" for example...

If I type this:
lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString().ToString( <-- the intellisense at this
point displays and I get two options...


Remove the *second* .ToString

Jan Hyde (VB MVP)

--
Isolate: Delay because of frozen roads (Ray Hand)

May 25 '06 #11

P: n/a
"Jan Hyde" wrote ...
Remove the *second* .ToString


Hi Jan,

That was only added to show you that without it, the first ToString only
gives me the (index) option - where-as if I put both, the second one allows
an IFormat thingy...

One thing I did try in the end, and it worked - although it seems over kill,
was to define a dateTime object, then populate that from my session object,
then do the ToString on that - sure enough I got the .ToString("dd/MM/yyyy")
ability - and it works - but it really does just seem over kill...

Any thoughts?

Regards

Rob
PS: I got a bit stuck with the other news reader - so many options kept
telling me it wasn't available in the free one - plus it seemed very slow at
getting message headers - it took my four attempts before I had all of the
replies to this thread!? Cheers though - it was nice to see something other
than Outlook Express's one.. :o)
May 25 '06 #12

P: n/a


assign to variable and use the method of string("mm/dd/yyyy")

*** Sent via Developersdex http://www.developersdex.com ***
Oct 18 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.