I'm confuse about how to make an input of formatted date time and currency. I want user to input the DoB as dd/mm/yyyy but when I'm using DateTime data type in Visual Studio it only get yyyy/mm/dd format.
Here's my code:
This is DoB and property from another class employee.cs
class employee
{
private DateTime myBOD;
public DateTime BOD
{
get
{
return myBOD;
}
set
{
myBOD = value;
}
}
}
This is the main form1.cs
vemployee.BOD = Convert.ToDateTime(bod.Text);
var today = DateTime.Today;
age.Text = Convert.ToString(today.Year-vemployee.BOD.Year);
Well, DateTime is a struct it doesn't have any format but properties like Year, Month, Day etc.
use DateTime.ParseExact when you want to obtain DateTime from string:
vemployee.BOD = DateTime.ParseExact(
bod.Text,
"dd'/'MM'/'yyyy", // Please, note that "mm" stands for minutes, not months
CultureInfo.InvariantCulture);
And .ToString(format) when you want to represent DateTime as a string
DateTime today = DateTime.Today;
bod.Text = today.ToString("dd'/'MM'/'yyyy", CultureInfo.InvariantCulture);
Related
I have a date in this format "2017-03-29" and time like "09:30", How do I conver toDatetime.
Following is how I have
string date = "2017-03-29";
string time = "09:30"
I need to convert this to DateTime in c#.
I also need to compare this converted DateTime with current dateTime, I will be using this in comparison in Linq
Use DateTime.ParseExact. Also your problem statement and code shown have nothing to do with Linq. The code below assumes the hours are in 24 hour format, adjust accordingly if that is not the case and provide an am/pm flag.
string date = "2017-03-29";
string time = "09:30";
var dateTime = DateTime.ParseExact(date+time, "yyyy-MM-ddHH:mm", null);
I would say the same as #Sam, but I don't have enough reputation to comment.
string date = "2017-03-29";
string time = "09:30";
string dateTimeString = string.Format("{0} {1}", date, time);
DateTime dateTime = DateTime.ParseExact(dateTimeString, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
Note that the Kind of the resulting DateTime is DateTimeKind.Unspecified. Convert it as necessary.
Using the variables provided:
string dateTime = date + " " + time;
DateTime d = Convert.ToDateTime(dateTime);
I have the Date and Time like this 2016/11/28 and time 07:30 PM. And combine this string and make like below
string mydate = extras.GetString("Apodate") + " " + extras.GetString("Apostarttime");
so mydate string contain 2016/11/28 07:30PM.
No I want to convert this string to below format
"yyyy-MM-dd'T'HH:mm:ss'Z'"
So I try this way:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try
{
Java.Util.Date startdate = dateFormat.Parse(mydate);
Java.Util.Date enddate = dateFormat.Parse(mydate1);
SimpleDateFormat rformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTime dt1 = DateTime.ParseExact(rformat.Format(startdate).ToString(), "yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
model.StartTime = dt1;
}
catch (ParseException e)
{
e.PrintStackTrace();
}
But my model.StartTime contain 12/11/0195 7:30:00 PM. But I want
2016/11/28 7:30:00 PM. as a DateTime.
First the thing you should consider to do is
1. Convert String(type) To DateTime(type)
- Search how parseExact do first, I think "yyyy-MM-dd'T'HH:mm:ss'Z'" is not the correct datetime format
2. When you got Datetime(type) you can display to any string format you want
- Search how to convert type DateTime to string
Link
https://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp#=
How do I get the AM/PM value from a DateTime?
TLDR;
SimpleDateFormat rformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");//Format text as what ParseExact method can do
DateTime dt1 = DateTime.ParseExact(rformat.Format(startdate).ToString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);//plz become Datetime!!! And don't give me a runtime error here
model.StartTime = dt1;
And when you want to display model.StartTime
string Datetxt = model.StartTime.ToString("yyyy/MM/dd'T'HH:mm tt'Z'", CultureInfo.InstalledUICulture) //now become a beautiful string with unwanted chars 'T' and 'Z'
I have a set of array.
//this is not hard corded, some times array will have multiple no.of strings in date format.
["vishnu","2016-08-31T18:30:00.000Z","1992","banglore"]
I have an array of strings, among these strings there is one string which is in date format.
I need to do a foreach and need to check which string is in the date format.
If we got the date string "2016-08-30T18:30:00.000Z" I need to convert it to basic date format but in correct timezone, here the date is 2016-08-31 but what I need as out put is
["vishnu","31/8/2016","1992","banglore"]
not
//check the difference in date!
["vishnu","30/8/2016","1992","banglore"]
the aim is from the array, if string is in date string format, convert it.
public static void Main(string[] args)
{
string inputString = "2016-08-31T18:30:00.000Z";
DateTime enteredDate = DateTime.Parse(inputString);
Console.WriteLine(enteredDate);
DateTime dDate;
if (DateTime.TryParse(inputString, out dDate))
{
DateTime dtx = enteredDate.ToLocalTime();
String.Format("{0:d/MM/yyyy}", dDate);
Console.WriteLine(dtx);
}
else
{
Console.WriteLine("Invalid"); // <-- Control flow goes here
}
// DateTime dt = convertedDate.ToLocalTime();
}
If you need to correct the DateTime for the time zone, you can use TimezoneInfo.ConvertTime():
string inputString = "2016-08-31T18:30:00.000Z";
DateTime dDate;
if (DateTime.TryParse(inputString, out dDate))
{
DateTime correctedDateTime = TimeZoneInfo.ConvertTime(dDate, TimeZoneInfo.Local);
// write this here back into the array using your format
Console.WriteLine(correctedDateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
}
else
{
Console.WriteLine("Invalid"); // <-- Control flow goes here
}
For further reference check out this post. This answer is inspired by it to use TimeZoneInfo.
DateTime dDate;
do this operation iside foreach
if (DateTime.TryParse(answerString, out dDate))
{
DateTime enteredDate = DateTime.Parse(answerString);
var Date = enteredDate.ToString("dd/MM/yyyy");
answerString = Date;
Console.WriteLine(answerString);
}
else{
//operation
}
thanks to mong zhu
Try using DateTimeOffset rather than DateTime as it is built to handle time zones.
Here's the code:
string inputString = "2016-08-31T18:30:00.000Z";
DateTimeOffset enteredDate = DateTimeOffset.Parse(inputString);
Console.WriteLine(enteredDate);
DateTimeOffset dtx = enteredDate.ToLocalTime();
Console.WriteLine(dtx);
This produces the following for me in GMT+09:30:
2016/08/31 18:30:00 +00:00
2016/09/01 04:00:00 +09:30
To get it in Indian time try this:
DateTimeOffset dtx = enteredDate.ToOffset(TimeSpan.FromHours(5.5));
Console.WriteLine(dtx);
I get 2016/09/01 00:00:00 +05:30 now.
when i am trying to connect c# code to mysql database there is a mistake in date datatype
try
{
CultureInfo CultureInfoDateCulture = new CultureInfo("ja-JP");
DateTime d = DateTime.ParseExact(accountend, "yyyy/MM/dd", CultureInfoDateCulture);
return true;
}
catch
{
return false;
}
when i have to collect date from the month calender i have used this validation for MYSQL and the datatype i have used is DATE datatpe
I have been getting the folllowing error
String was not recognised as a valid date time
please help me guys.... Thanks in advance
MYSQL Date Format is yyyy-MM-dd so you should use the same format while parsing it in the DateTime.ParseExact() method.
Try This:
DateTime d = DateTime.ParseExact(accountend, "yyyy-MM-dd",
CultureInfoDateCulture);
EDIT : From Your Comments your Date Format is M/d/yyyy
Try This:
DateTime d = DateTime.ParseExact(accountend, "M/d/yyyy",
CultureInfoDateCulture);
DateTime.ParseExact requires that the format string represent accurately the day, month and year of the string that need to be converted
So assuming that your date string is 4/11/2014 (day, month, year) you need
try
{
CultureInfo CultureInfoDateCulture = new CultureInfo("ja-JP");
DateTime d = DateTime.ParseExact(accountend, "d/M/yyyy", CultureInfoDateCulture);
return true;
}
catch
{
return false;
}
instead if the format is month, day, year, the format string is
DateTime d = DateTime.ParseExact(accountend, "M/d/yyyy", CultureInfoDateCulture);
By the way, your date is in the format expected by the InvariantCulture, so you could avoid the creation of the CultureInfo in this context, it is enough to use
DateTime d = DateTime.Parse(accountend, CultureInfo.InvariantCulture);
I have been trying to get a date conversion to wrok to convert from a string to a date I have looked at msdn and some other stack o questions but multiple ways have not worked. I am making a console app and it needs a valid date to check other dates. below is my current attempt.
string StartDate, EndDate;
Console.WriteLine("Input Start date");
StartDate = Console.ReadLine();
StartDate = DateTime.Parse(StartDate);
I currently set the variable StartDate and then set it a value depending on what the user enters and then it should change this to a date using the Parse
you are trying to assign the DateTime value to string StartDate, which is wrong. So change it like below:
string StartDate, EndDate;
DateTime date;
Console.WriteLine("Input Start date");
StartDate = Console.ReadLine();
date = DateTime.Parse(StartDate);
A string is not a DateTime and a DateTime not a String. So you might be able to parse a string to a date but you cannot use the string variable for the DateTime and vice-versa. You need two variables:
string startDateInput = Console.ReadLine();
DateTime startDate = DateTime.Parse( startDateInput );
Since this could fail if the input string is not a valid date you should use TryParse:
DateTime startDate;
bool validDate = DateTime.TryParse(startDateInput, out startDate);
if(validDate)
Console.Write("Valid date: " + startDate.ToLongDateString());
Try using Convert.ToDateTime();
Example:
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);
Use DateTime.TryParse()
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
DateTime date;
if (!DateTime.TryParse("DateString", out date))
{
MessageBox.Show("Invalid string!");
}
You need to specify the Date Format.
Try This: Example format MM-dd-yyyy
Console.WriteLine("Input Start date Format -> MM-dd-yyyy");
string StartDate = Console.ReadLine();
DateTime YourDate = DateTime.ParseExact(StartDate,"MM-dd-yyyy",
System.Globalization.CultureInfo.InvariantCulture);