Comparing two datetime - asp.net

I send a datetime using query $post to Mvc Controller;
1- On the server machine it doesn't bind well , some time in wrong format
I solved it by sending datetime as string then split the string to day, month , year.
2- Second problem; I parsed the datetime but when I compare it with another one it gives me wrong result.

Post your date/time formatted in the ISO standard.
var myDate = new Date();
var dateString = myDate.toISOString();
Post dateString to your api.

Related

Sending http request and getting error in response : "String was not recognized as a valid DateTime."

I am sending an xml message in http request method. The date format which is required is MM/dd/yyyy. I sending the value of dob in the required format but still it send me an error of datetime. I couldn't figure out why. Moreover I am using an api so these are the requirements of api to send date time in this format.
CultureInfo us = new CultureInfo("en-US", true);
DateTime mydate = new DateTime();
// yyyy-MM-dd (2016-10-06) recieved from text box
mydate = DateTime.ParseExact(dateOfBirthTextbox.Text.ToString(), "yyyy-MM-dd", us);
String dob = mydate.ToString("MM/dd/yyyy");
Try this.....
Parse the DOB using Datetime.ParseExtract and specify the date format.. it will convert string date to proper date format.

Google Datastore -> Compare DateTime-Property with Date

I just can't find an answer to the following problem:
I have entities in my Google App Engine Datastore which all have a property of the type "Date and time". My Datastore console shows me the format of that property. It looks like this:
2/16/16, 7:20 PM CET
Now I want to do a query that only gives back the entities AFTER a specific date. But how do I format that date? I tried the following:
#ApiMethod(name = "getUpdatedEpisodes", path = "getUpdatedEpisodes")
public List<Episode> getUpdatedEpisodes(#Named("lastUpdate") String lastUpdate) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy, hh:mm a z");
Date date = null;
try{
date = sdf.parse(lastUpdate);
}catch (Exception ex){
}
Query.Filter filter =
new Query.FilterPredicate("lastUpdate",
Query.FilterOperator.GREATER_THAN,
date);
Query q = new Query("Episode").setFilter(filter);
PreparedQuery pq = datastore.prepare(q);
Afterwards I try to set a new date and run this method again. I do the following:
repository.getInstance(context).lastUpdate = DateTime.now(DateTimeZone.forID("CET")).toString("dd/MM/yy, hh:mm a z");
While debugging I can see that this String looks like
2/16/16, 7:24 nachm. GMT+01:00
And if I try my API with this string again, I will still get all my episodes, so no filtering was done. Is this because my Date and the Google Date can't be compared if they aren't the exact same format? How can I fix this?
Rather than using a String for your date property, you can use a Date object:
import java.util.Date;
...
repository.getInstance(context).lastUpdate = new Date();
The Datastore will store this property so that it is sorted in chronological (or anti-chronological) order and query filters will work as expected.
https://cloud.google.com/appengine/docs/java/datastore/entities#Java_Properties_and_value_types
I solved this issue by storing my date in the datastore as millis instead of date and time. This way I only have an 64-bit integer that is easy to compare.

Converting any date format in MM/DD/YYYY format

I am having difficulty with different date formats. I am having a web application created using asp.net c#. I have used JQuery Calendar control which shows date along-with time.
I am then parsing this date using DateTime.Parse(). If the server is set with indian date format then the application generates conversion error "String was not recognized as a valid DateTime."
Is there a way to convert any date format into MM/DD/YYYY format along with time?
I know i can use DateTime.ParseExact() but again it will stuck with a particular format.
Any help is greatly appreciated.
Thanks for sharing you wisdom.
Convert.ToDateTime(DateTime.Now, CultureInfo.GetCultureInfo("en-US")).ToString("MM/dd/yyyy hh:MM:ss");
You can replace en-US by hi-IN for Hindi Date or as per you like. please follow link. for Culture Info.
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.80).aspx
If you are unsure of Culture then use below line.
Convert.ToDateTime(DateTime.Now, CultureInfo.CurrentCulture).ToString("MM/dd/yyyy hh:MM:ss");
reference here http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx
Other Method you can use is.
DateTime dt;
DateTime.TryParse(DateTime.Now.ToString(), out dt);
You can try DateTime.Parse or DateTime.TryParse methods
DateTime.Parse(DateTime.Now).ToString("MM/dd/yyyy hh:mm tt");
Also refer
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Thanks
Deepu
You can create an Extension methods for this you need to create a static class with static method. It will return you a date format that you define in class
public static class DateExtension
{
public static string GetDateTime(this DateTime? date)
{
if (date.HasValue)
return date.Value.ToString("dd-MMM-yyyy hh:mm tt");
return string.Empty;
}
}
Now in you code you can use this extension for e.g. DateTime.GetDateTime() it will return date with time that with the format you specify in your extension library class
string s;
s = dt.ToString("MM-dd-yyyy");
Console.WriteLine(s);//Displays 04-30-2012
You can use this code.
public static CultureInfo CreateCulture(string shortDateFormat)
{
CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
newCulture.DateTimeFormat.ShortDatePattern = shortDateFormat;
newCulture.DateTimeFormat.LongDatePattern = shortDateFormat;
return newCulture;
}

showing DateTime in simple format?

i have a column in my database that stores DateTime, now i want to show time in simple format,
you know it will appear like : 12/26/2011 9:39:55 PM
but i want to show it in persian.
When you want to format the DateTime in your application, you need to use a DateTime.ToString overload.
Select the appropriate standard Date and Time format string - in this case "g" looks like a good fit.
If you have not set up your webserver to use the fa-IR culture by default, you will need to pass this culture in as well.
string farsiDate = myDateTime.ToString("g", CultureInfo.GetCultureInfo("fa-IR");
Have a look at the examples on MSDN for using the PersianCalendar class.
var cal = new PersianCalendar();
var today = DateTime.Now;
var persianDate = string.Format("{0}/{1}/{2}",
cal.GetDayOfMonth(today),
cal.GetMonth(today),
cal.GetYear(today));

Convert string date to system datetime in C#

I want to convert string date to system datetime in C#. The user enters date through textbox and it should be converted as to datetime. I trid following code but its not working...
DateTime ToDate = Convert.ToDateTime(txtToDate.Text);
DateTime FromDate = DateTime.Parse(txtFromDate.Text);
It shows the following exception
"String was not recognized as a valid DateTime."
How to do this...???
You could use DateTime.ParseExact(). That way you can specify the format of the input string, so it will be parsed correctly, for example:
dateString = "Sun 15 Jun 2008";
format = "ddd dd MMM yyyy";
try
{
DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.CurrentCulture);
}
catch (FormatException)
{
}
whatever user enters in your textbox that should be in valid date format, otherwise write your own function to make it in valid format. then Convert it into DateTime format .
for different format you can check this :
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
for more help you can check similar question on this site :
Validate a DateTime in C#
You can of course parse the user's input and rely on the users to always enter a correct date. But I'd recommend to use a specific control for entering a date, such as the calendar control of the ajax control toolkit.
By using such a control, you can prevent invalid input and it's also much easier for the user. If you search for DatePicker or similar, I'm sure you can find lot's of other similar controls.
Ask the user to enter his datetime in a particular format into textbox i.e., either "ddMMyyyyhhmmss" or "dd/MM/yyyy hh:mm:ss tt" or "dd-MM-yyyy hh:mm:ss tt" or some other formats and use the help of following code to convert in to a Valid datetime.
DateTime ToDate = DateTime.ParseExact(txtToDate.Text, <User DateTime format as String>,
System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
Instead, if the above coding makes complicated then you can try DateTime.TryParse() also
First of all you have to validate text box value that it is valid or not, you can use ajax MaskeditExtender control for that and restrict the use enter only require date formate.
DateTime dt = Convert.ToDateTime(date);
here date is in string.

Resources