Click here to Skip to main content
15,891,841 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to insert current system date into table on button_click event
Posted
Updated 4-Mar-18 0:27am

for retrieving current date you can use getdate() function in sql .
for the various date formats refer:
refer:

[^]
http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx[^]
http://www.simple-talk.com/sql/learn-sql-server/robyn-pages-sql-server-datetime-workbench/[^]
 
Share this answer
 
Its always better to Tag a Question with all possible related technologies(including RDBMS) you are using.

I do not recommend you to use .Net/C# syntax/code to save Current Date. Because on Production, Time-Zone of your Web-Server and Database-Server may not be same.

Use below syntax to get Current DateTime of your DB-Server.

1) SQL Server
http://www.w3schools.com/sql/func_getdate.asp

2) Oracle
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions172.htm

3) MySQL
http://www.w3schools.com/sql/func_now.asp

4) DB2
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=%2Fcom.ibm.db2.luw.sql.ref.doc%2Fdoc%2Fr0005870.html
 
Share this answer
 
For SQL : getdate();

For C# : DateTime.Now; or DateTime.ToDay;

If the field is varchar then,

DateTime.Now.ToString("dd/MM/yyyy");
or
DateTime.ToDay.ToString("dd/MM/yyyy");
 
Share this answer
 
v2
Comments
RaviRanjanKr 17-Nov-11 3:07am    
[Edited]Formated your answer[/Edited]
A suggestion :- Always use "pre" tag or Code block to wrap your codes.
if the datatype of your date column in your database is datetime then in button click you can use directly

DateTime.Now


or if it is varchar type you can pass like this

XML
DateTime.Now.ToString()



or if you write stored procedure for inserting the record in stored procedure you can write like
getdate() function in sqlserver returns the current date and time
getdate()
 
Share this answer
 
Comments
Richard MacCutchan 17-Nov-11 5:37am    
Never store dates as strings in a database.
You can do it byusing Codebehind or SQL

using SQL
SQL
SELECT GETDATE()


using code behind
DateTime.Now.Date.ToString("MM/dd/yyyy") ;
 
Share this answer
 
I cannot provide whole code. Use Getdate() function to get Current datetime function.

Like

SQL
Insert Into Abc(FieldName) Values (getdate())
 
Share this answer
 
DateTime.ParseExact(yourControlId,"dd/MM/yyyy",null);
 
Share this answer
 
hi,

use
select GetDate() function



DateTime.Now.Tostring("mm/dd/yyyy");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900