c# check date is weekend or weekday - c#

DayOfWeek today = DateTime.Today.DayOfWeek;
if (today == DayOfWeek.Sunday || today == DayOfWeek.Saturday)
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" weekend"');", true);
}
else
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" weekday"');", true);
}
From line above i am able to find if today is weekend or weekday
now i want to get the date from user input
i tried this but fail,
my text input format : 2016-10-04
string dateInput = dateTextbox.Text;
DayOfWeek today = DateTime.dateInput.DayOfWeek;

Use DateTime.ParseExact to parse your string into a DateTime
string dateInput = dateTextbox.Text; //"2016-10-04"
DateTime dtResult = DateTime.ParseExact(dateInput, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DayOfWeek today = dtResult.DayOfWeek;

IMO, you should use some DateTime control instead of a TextBox to enter DateTime. till then you need to convert TextBox Text to DateTime object first.
DateTime dt = DateTime.ParseExact(dateTextbox.Text,
"YYYY-MM-DD",
CultureInfo.InvariantCulture);
FYI, there are numerous ways to convert String to DateTime (google will help). All have their pros and cons. Use which best suits you.

Related

comparing date values for equality

I have a text box where user enters date in format "YYYY-MM-DD". I have to check whether the entered date is current date as per the server date. If both the date matches then only proceed further. I have to check only the date not date time.
<input type="text" class="form-control input_text" runat="server" id="englishDate9"/>
So how can I check this, is it possible to use asp validator or I have to do it from code behind?
#region date check section
string sysDt = englishDate9.Value;
DateTime oDate = DateTime.Parse(sysDt).Date;
DateTime sysdate = DateTime.Now.Date;
if(oDate == sysdate)
{
// show message
}
#endregion
I am using this code, but I am confused is this the correct code or not although for now it is giving correct result for me?
Hope this will help.
DateTime oDate;
if(DateTime.TryParse(englishDate9.Value, out oDate))
{
if(oDate != DateTime.Today)
{
return;
}
}
else
{
return;
}
You should do like this. because string any be anything thing other than the valid date and the required format.
string dateString = "2009-06-20";
DateTime dt;
bool isValid = DateTime.TryParseExact(dateString, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
DateTime sysdate = DateTime.Now.Date;
if (isValid == true && DateTime.Now.Date == sysdate)
{
//Do something
}

Convert Java Date to DateTime not work in Xamarin

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'

Error : String was not recognised as a valid date time

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);

Converting an inputted string into a date

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);

How to compare two dates in c#

I have a textbox where i will enter date in mm/dd/yyyy format,i want to compare this textbox date with current date(sysdate) in javascript code in c#.How to do that??Example i enter 11/11/2011 which is future date and it must be checked with sysdate and print an message
example
DateTime.ParseExact(txtDate.Text, "mm/dd/yyyy",null) > DateTime.Today
DateTime dateValue;
string dateString = textBox.Text;
if (DateTime.TryParse(dateString, out dateValue))
{
// Use Date.CompareTo as you want to...
int result = DateTime.Compare(dateValue, DateTime.Today);
if (result > 0)
{
//Future Date
}
}

Categories

Resources