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

R

Rob Meade

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 :eek:)
 
J

Jan Hyde

"Rob Meade" <[email protected]>'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 :eek:)

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)
 
R

Rob Meade

...
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,
 
R

Rob Meade

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
 
J

Jan Hyde

"Rob Meade" <[email protected]>'s wild thoughts
were released on Mon, 22 May 2006 11:49:06 +0100 bearing the
following fruit:
...


Hi Jan, thank you for your reply.

Clearly I didn't explain myself very well...my apologies.


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)
 
J

Jan Hyde

"Rob Meade" <[email protected]>'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..


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)
 
R

Rob Meade

...
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 :eek:)

Rob
 
R

Rob Meade

...
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
 
J

Jan Hyde

"Rob Meade" <[email protected]>'s wild thoughts
were released on Mon, 22 May 2006 15:28:31 +0100 bearing the
following fruit:
...


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)
 
R

Rob Meade

...
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
 
J

Jan Hyde

"Rob Meade" <[email protected]>'s wild
thoughts were released on Tue, 23 May 2006 18:31:39 GMT
bearing the following fruit:
...


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)
 
R

Rob Meade

...
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.. :eek:)
 
Joined
Mar 26, 2007
Messages
1
Reaction score
0
Problems with date and time

Hi all;

I have Parameter with type string and the value date and time like this ("25/12/2007 12:00:00") and I'm using ASP.Net 2.0

only i want from this string is the date ("25/12/2007")

so can you help me
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top