how to get current date and insert database in asp.net

Posted by Gow.Net under ASP.NET on 10/17/2012 | Points: 10 | Views : 23333 | Status : [Member] | Replies : 6
how to get current date and insert database in asp.net

gowthaman8870226416


Responses

Posted by: Vasanthmvp on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
we have many functions in DB like
1. GETDATE() --> gets you the current date and time
2. DatePart() --> Returns int type i.e., time, date, smalldatetime, datetime, datetime2, or datetimeoffset value etc.,
3. DateDiff() the difference of two dates --> result can be drawn in minutes, hours, days etc

To Insert Date into Database,
Suppose you have a table(Demo) with column TodayDate(Name) DateTime or Date (datatype).
Query: Insert INTO Demo(TodayDate) values (GetDate()) gets you the current date time and inserts into the table.

Regards,

Awesome Coding !! :)

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down
Datetime dt=new Datetime.Now;
String dt=dt.Tostring("yyyy-mm-dd");

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gow.Net on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down

hi Shanky11 thanks for your replay

bu

DateTime dt=DateTime.Now;
here dt i got dt = {10/17/2012 1:25:53 PM}
month=10

String datetoday = dt.ToString("yyyy-mm-dd");

now i got datetoday="2012-25-17"
month=25
here please note it month will be change
how to solve this

gowthaman8870226416

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,


This following code will give you the current date.
string strDate = DateTime.Now.ToString("yyyy-MM-dd");

Thanks && Regards,
V. Santhi.

Thanks & Regards,
Santhi .V

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 10/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gow.Net,
DateTime.Now.ToShortDateString() in the aspx page, and DateTime or Date as datatype in the database will insert correctly. No need to change to any formats.

Regards,

Awesome Coding !! :)

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 10/17/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Gow.Net,
try this CODE


DateTime dt = DateTime.Now;
Response.Write(dt.ToString("MM/dd/yyyy"));
or
DateTime dt = DateTime.Now;
Response.Write(dt.ToString("dd/MM/yyyy"));


Mark as Answer if its helpful to you

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response