DateTime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

There is a datetime type with length 8 in a table

I tried to insert the Local System datetime to it from aspx.cs file. But, the outcome are

string sysdatetime = System.DateTime.Now.ToShortDateString(); //output 1900-01-01 00:00:00.000 in tabl
string sysdatetime = System.DateTime.Now.ToLongDateString(); //output 1900-01-01 00:00:00.000 in tabl
DateTime sysdatetime = System.DateTime.Now; //Compile error. Cannot convert char to datetim

They cannot output today datetime. How should I fix it

And, how can I get only Date 1900-01-01, only Time 00:00:00 or both DateTime 1900-01-01 00:00:00

Thanks
 
play with "custom DateTime Format Strings" (refer to MSDN for complete
description)

in your case:
DateTime.Now.ToString("yyy-MM-dd");
DateTime.Now.ToString("hh:mm:ss");

José

Tom said:
Hi,

There is a datetime type with length 8 in a table.

I tried to insert the Local System datetime to it from aspx.cs file. But, the outcome are:

string sysdatetime = System.DateTime.Now.ToShortDateString(); //output
1900-01-01 00:00:00.000 in table
string sysdatetime = System.DateTime.Now.ToLongDateString(); //output
1900-01-01 00:00:00.000 in table
 
Hi

I use the following syntax

System.DateTime.Now.ToString("yyy-MM-dd hh:mm:ss")

The data in table changes from 1900-01-01 00:00:00.000 to 1/1/1900

It cannot retrieve the current datetime of the OS.
 
I'm not too sure what you are trying to do...
However, I made a mistake, it should be
System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

As I see it you store your info inside a dB. Do you use a store procedure to
do it?
Assuming yes and that you use a dateTime as sql type, you could do the
following:
...
// Get a command object and make it part of transaction if needed
SqlCommand SqlCmd = new SqlCommand("UpdateCredentialsForTarget",
m_dBConnect, m_dBTrans);

SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.Parameters.Add("@myDate", SqlDbType.DateTime).Value = DateTime.Now;


// Execute the SP
SqlCmd.ExecuteNonQuery();

José
 
Tom said:
I use the following syntax:

System.DateTime.Now.ToString("yyy-MM-dd hh:mm:ss");

The data in table changes from 1900-01-01 00:00:00.000 to 1/1/1900

It cannot retrieve the current datetime of the OS.

When you say "table", what exactly is your context? Is it a DataTable?
If so, you should make the type of your column a DateTime rather than
String.
 
The table schema

userid int
registerdate datetime

The cs clas

protected System.DateTime CurDateTime
CurDateTime = System.DateTime.Now

The SQL i

INSERT INTO tablename (registerdate) values (this.CurDateTime); //it returns erro

I did not use stored procedure

How should I fix it
 
I tried CurDateTime.ToString();

It insert 1/1/1900 as DateTime

Why not current date and time?
 
My guess is that that date is the default datetime when an invalid
datetime format is passed. Look in the SQL docs for how the string
needs to be formatted, and then call the ToString("...") with the
proper format.

Austin Ehlers
 
Tom said:
The table schema:

userid int 4
registerdate datetime 8

The cs class

protected System.DateTime CurDateTime;
CurDateTime = System.DateTime.Now;

The SQL is

INSERT INTO tablename (registerdate) values (this.CurDateTime); //it
returns error

I did not use stored procedure.

How should I fix it?

You should use a parameter in your command instead, and then set the
value of the parameter to the DateTime value you want. That way the
drivers deal with all the conversions for you.
 
Back
Top