How Read Access Database Date Time Format as Date Only - c#

I have used like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString(), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
I have also tried like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString("dd/MM/yyyy"), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
but it is not reflected. I also changed in access database format as Short date, but that is also not gave correct answer.

You can use dueDate.Date
DateTime.Date Property
Gets the date component of this instance.
Example
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
Results
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
//
You can play with a demo here

Try this
DateTime dueDateTime = Convert.ToDateTime(row["Date required"].ToString());
DateTime dueDate= dueDateTime.Date;
First Convert it into Stting and then to DateTime
and then get Date from DateTime

Related

How to Convert Long format Date to Short format Date with Type DateTime in C#

I want convert long dateTime to only date in C#. and both in dateTime type
DateTime creationDate = DateTime.Now;
DateTime shortDate = creeationDate.Date;
A DateTime is a struct, which always has -- as the name suggests -- date and time components. You cannot remove the time and there is, as far as I know, no built-in date-only type in .NET (maybe there is in some 3rd party libraries).
You have already discovered the Date property of DateTime which returns an new DateTime with the time components set to 00:00:00.000.
If you only need to output your current date you must apply some formatting on the DateTime
DateTime creationDate = DateTime.Now;
string shortDate = creatationDate.ToString("yyyy-MM-dd"); //or whatever formatting you need.
If you need calculations on your date, but want to ignore the time component, you could initialize your date accordingly, as you have done in your code.
DateTime creationDate = DateTime.Now.Date; //sets time component to 00:00
DateTime otherDate = new DateTime(2016, 11, 2, 0,0,0); //also initializies with time 00:00
int diff = (int)(otherDate-creationDate).TotalDays; //number of days between the two dates

How can I format e.Day.Date command of calendar show only Date?

I want to use only Date for check my function but it always get the time follow or can I format timezone of e.Day.Date.Year to show timezone what i want ? 3
This is example of my code the first code at e.Day.Date.Year of year is "en-US"
but i want to change timezone of the value can i ?
g = (e.Day.Date.Day + "/"+ e.Day.Date.Month + "/" + e.Day.Date.Year)
and
This line value of e.Day.Date always have time follow the date with 00:00:00 it can't check with the other time
if(e.Day.Date == dt)
You probably want DateTime.Date rather than .Day
See here: https://msdn.microsoft.com/en-us/library/wbed0aaa%28v=vs.110%29.aspx
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
}
}
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00

calling specific DateTime date as an input argument, c#

How do I use a specific date as an input value?
var experiment1 = WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
Later on, I'll be convering it to a string, but I need it to be input as a DateTime.
public static string GetDisplayString(string city, DateTime date, double temp)
{
}
I'm using DateTime.Today as a placeholder, simply because it works. The thing is, I need it input as a specific day of a month of a year. (I've tried using (10, 10, 10) but it simply gives me a compiler error.
Edit.: I can't believe I did not figure out all I need to do is to add "new". Thanks folks
In (10, 10, 10) do you want the year 10?
Use the constructor that takes in a year, a month and a day. As below.
DateTime dt = new DateTime(2013, 12, 12);
then call your method
var experiment1 = WorkingWithDates.GetDisplayString("London", dt, 45.00);
PS: If you want year 10
DateTime dt = new DateTime(10, 10, 10);
will work. The year will be 0010
WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
you need a DateTime object to be used in place of DateTime.Today, so construct your instance using
DateTime newdate = new DateTime(2013,10,10) // year, month , day
and then
WorkingWithDates.GetDisplayString("London", newdate, 45.00);

Datetime format for a month and year

DateTime ExpMonth = Convert.ToInt32(ddExpMonth); ---- DropDown(user selects a month)
DateTime ExpYear = Convert.ToInt32(ddExpYear); ---- Dropdown(user selects year)
Datetime ExpDate = ///// I want this part to be saved as Datetime 02/2012
How is this possible. Or any other way.
A DateTime value doesn't know about a format - it's just a date and a time. You can create a new DateTime value with the relevant information:
DateTime expiry = new DateTime(Convert.ToInt32(ddExpYear),
Convert.ToInt32(ddExpMonth),
1);
... but how that is "saved" is entirely up to you. If you give us more information, we may be able to help you more. You can format it to a string easily enough:
string formatted = expiry.ToString("yyyy/MM");
... but that may not be what you're after.
You could store it in a DateTime as follows:
DateTime expDate = new DateTime(ExpYear, ExpMonth, 1).AddMonths(1).AddDays(-1);
If it's for a credit card expiration date, make sure the day is the last day of the month or don't compare the day. Their may be some discrepancies on the last day being expired or not. It should be still valid so make sure the current date is at least a day greater.
You will need to save this value either as a nvarchar, where you'll be able to do whatever you want, or a datetime. The difference is that the datetime format requires you to provide the day, and the time be set to midnight. The value 1, for the first day of the month should be considered here.
DateTime Today = new DateTime( DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
DateTime cc = new DateTime(2016, 9, 1).AddMonths(1).AddDays(-1);
Console.WriteLine(Today.ToString());
Console.WriteLine(cc.ToString());
if (Today <= cc)
{
Console.WriteLine("Ok");
}
else
{
Console.WriteLine("Card Expiry Date is not valid ");
}

C# Getting Just Date From Timestamp

If I have a timestamp in the form: yyyy-mm-dd hh:mm:ss:mmm
How can I just extract the date from the timestamp?
For instance, if a timestamp reads: "2010-05-18 08:36:52:236" what is the best way to just get 2010-05-18 from it.
What I'm trying to do is isolate the date portion of the timestamp, define a custom time for it to create a new time stamp. Is there a more efficient way to define the time of the timestamp without first taking out the date, and then adding a new time?
DateTime.Parse("2010-05-18 08:36:52:236").ToString("yyyy-MM-dd");
You should use the DateTime type:
DateTime original = DateTime.Parse(str);
DateTime modified = original.Date + new TimeSpan(13, 15, 00);
string str = modified.ToString("yyyy-MM-dd HH:mm:ss:fff");
Your format is non-standard, so you'll need to call ParseExact instead of Parse:
DateTime original = DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture);
You could use substring:
"2010-05-18 08:36:52:236".Substring(0, 10);
Or use ParseExact:
DateTime.ParseExact("2010-05-18 08:36:52:236",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd");
DateTime date;
if (DateTime.TryParse(dateString, out date))
{
date = date.Date; // Get's the date-only component.
// Do something cool.
}
else
{
// Flip out because you didn't get a real date.
}
Get the .Date member on the DateTime
DateTime date = DateTime.Now;
DateTime midnightDate = date.Date;
use it like this:
var x = DateTime.Now.Date; //will give you midnight today
x.AddDays(1).AddTicks(-1); //use these method calls to modify the date to whats needed.
The best (and fastest) way to do this is to convert the date to an integer as the time part is stored in the decimal part.
Try this:
select convert(datetime,convert(int, #yourdate))
So you convert it to an integer and then back to a data and voila, time part is gone.
Of course subtracting this result from the original value will give you the time part only.

Categories

Resources