How to create a variable in C# to hold date? [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Can someone give me the code that has 2 variables. 1 variable holds the date & the other displays in longdate format.

Simple:
DateTime date = DateTime.Now; // will give the date for today
string dateWithFormat = date.ToLongDateString();
You can further modify how the format of the string. You can do this by date.ToString("dd - MM - yyyy"); which would output something like 09 - 15 - 2011
You can read about this here

Use this
DateTime date = DateTime.Now;
string longDate = date.ToLongDateString();

Use this code
DateTime myDate = DateTime.Now;
string longDateString = myDate.ToLongDateString();

Related

Convert DateTime Format with Millisecond in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
This is date time format i want "2013-06-25 18:46:54.687" to pass in to sql server.
How to convert in C#?
DateTime LastUpdateTime=Convert.toDateTime(LastUpdateTime);
//2013-06-25 18:46:54.687 with 3 index of millisecond
sc.Parameters.Add("#LastUpdateTime", SqlDbType.DateTime).Value = LastUpdateTime;
You're using the wrong Data Typ in SQL.
The datetime2 can be considered as an extension of the existing datetime type that has a larger default fractional precision, and optional user-specified precision.
C# Format Milliseconds exactly the way you want.
In an Example:
DateTime date2 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date: {0:o}",
date2);
// Displays the following output to the console:
// Date: 2008-01-01T00:30:45.1250000
Look at Why is SQL Server losing a millisecond? and DateTime2 vs DateTime in SQL Server
These are great Question with good Answers, BTW.
If your database query parameter is string, use following format:
LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")
Otherwise, it would be better to send datetime as original object,
and let sql server do all conversions.

how to check if sql table date in type string is lower then today's date in string C# winform [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a table with a date but it is in type string not type date (dd/mm/yyyy).
I want to get from the table all of the rows that the date is lower then today's date,
I have today's date in string and the date in the table in string,
so how do i check who is bigger?
FOR SQL SERVER
You need to cast it to date in order for you to compare the records.
SELECT *
FROM TableName
WHERE convert(date, [date], 103) < GETDATE()
SQLFiddle Demo
FOR MYSQL
You need to convert those values into date datatype using STR_TO_DATE in MySQL.
SELECT *
FROM TableName
WHERE STR_TO_DATE(date, '%d/%m/%Y') < CURDATE()
SQLFiddle Demo
STR_TO_DATE

Changing date-time format in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Creating digital clock. I'm newbie to C#. My code look like that
timeLbl.Text = DateTime.Now.ToLongTimeString();
dateLbl.Text = DateTime.Now.ToLongDateString();
Here is, the result
http://content.screencast.com/users/TT13/folders/Jing/media/da6d1f65-bf5f-4735-97dc-70485112a998/2012-07-02_1826.png
I got some questions:
Can I change time's format to 24 Hour? how?
How to change date into digits only format (like dd/mm/yyyy) or this result but in exact language (I mean, words "Monday, July" in another language, which windows support, for ex Turkish)?
How to make window dynamically change it's width (depending on text
length)?
Please help me,to achieve these 3 things. Thx in advance
Ans 1:
timeLbl.Text = DateTime.Now.ToString("HH:mm:ss");
Will convert time to 24 hour format.
Ans 2:
dateLbl.Text = DateTime.Now.ToString("dd/MM/yyyy");
Will convert date format to 31/06/2012
More formats here
The first and second point where been answered, for the last point set SizeToContent="WidthAndHeight" on the window and the window will dynamically resize based on its content size. I assume ur working with wpf, else it doesnt works!

DateTime Format from dd/mm/yyyy format to yyyy-mm-dd [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
how to convert date from dd/mm/yyyy format to yyyy-mm-dd using C#.net. please advice
You are looking for this:
d.ToString("yyyy-MM-dd");
Where d above is a DateTime.
Example:
DateTime d = DateTime.Now;
Console.WriteLine(d.ToString("yyyy-MM-dd"));
Console.WriteLine(d.ToString("dd/MM/yyyy"));
Prints:
2011-12-01
01/12/2011
Probably the most reliable way to do it is to parse your incoming string representing a date:
var date = DateTime.Parse("30/11/2011");
then get the string representation of the returned DateTime object:
var yyyy_mm_dd = date.ToString("yyyy-MM-dd");
yyyy_mm_dd will contain "2011-11-30"
Try doing DateTime.Now.ToString("yyyy-MM-dd");// this will return formatted as string
DateTime.Now.ToString("yyyy-MM-dd");
It's easy, take a look at this. Another way is just to do:
DateTime oldDT = DateTime.Parse("Date in old format"); // And then, later:
DateTime newDT = DateTime.Parse(oldDT.ToString("New format string"));
d.ToString("yyyy-MM-dd")
This will be useful to you.

How do you get the current time of day in datetime format? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How do I get the current time of day in datetime format?
You can use DateTime.TimeOfDay:
TimeSpan currentTime = DateTime.Now.TimeOfDay;
This will retrieve this as a TimeSpan. If you want this in a "DateTime" format, just use DateTime.Now (realizing, of course, that this also includes the date).
You can use DateTime.Now for that. Example:
$ cat Test.cs
using System;
namespace Test
{
public class TestClass
{
public static void Main(string[] args)
{
DateTime example = DateTime.Now;
Console.WriteLine(example);
}
}
}
$ gmcs *.cs
$ mono Test.exe
2/18/2011 11:03:13 PM

Categories