I have asp.net application and am using Entity Framework to connect it with the database. in this application, I have a textbox to get the date(am using calender css style here), and its in the string type.
I have a column in my database and its in Date Time format, I need to compare the textbox value with the date column in my database, for this I just used the code as
public StudentAttendances(string date)
{
if (date != "")
{
DateTime date1 = Convert.ToDateTime(date);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
for example if I select a date in my textbox(the format is 04/05/2012) and when I compare this with the database its not showing any data, but actually some datas are there for this date.
Your code is comparing both day and time (hours, minutes etc will have to match). Try comparing just the day part like this:
buDataEntities.StudentAttendances.Where(s => s.Date.Subtract(date1).Days == 0)
I also think that you should specify what format the input date from the users is in.
04/05/2012 may mean both 4th April or 5th of May depending on your computers regional setting.
Here is an example (below) for converting a date string in American format to DateTime object:
DateTime date1 = DateTime.Parse(date, new CultureInfo("en-US"));
Hope that helps!
your ask is very limited, but try to see this
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Please check whether the following works for you:
public StudentAttendances(string date)
{
if (date != "")
{
// please see the change from your given code
DateTime date1 = DateTime.ParseExact(date, "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
Related
I've added the Excel upload feature to the Details section of the Employee Time Card screen (EP305000). This works fine, but if the 'Time' field (which is actually date_time, but I can't find that in the DAC - only 'Date') isn't specified in the upload, it defaults to midnight (12:00 AM). I want this to default to 8:00 AM, but I'm not sure how to do this, since the field is actually a date. It doesn't seem like I can just use [PXDefault] or anything simple like that.
How can I accomplish this?
Thanks...
Here's the solution I came up with, using the 'RowInserted' event:
protected void EPTimeCardDetail_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
{
var eptcd = (EPTimecardDetail)e.Row;
DateTime theDate = (DateTime)eptcd.Date;
DateTime MyDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, 8, 0, 0);
eptcd.Date = MyDate;
}
In DOB i am getting date and time but i need only date
foreach (DataRow row in ProfileDt.Rows)
{
UserProfileData.FirstName = row["FirstName"].ToString();
UserProfileData.LastName = row["LastName"].ToString();
UserProfileData.Email = row["Email"].ToString();
UserProfileData.Address = row["HouseNo"].ToString();
UserProfileData.City = row["CityName"].ToString();
UserProfileData.date = row["DOB"].ToString();
}
If your database is giving you a string then you'll need to parse it to a date then back to a string with the desired date format.
string dateFormat = "MM/DD/YYYY HH:mm:ss" // REPLACE WITH THE CORRECT FORMAT
UserProfileData.date = DateTime.ParseExact(row["DOB"].ToString(), dateFormat ,
System.Globalization.CultureInfo.InvariantCulture).ToString("MM/DD/YYYY");
If your database is giving you a datetime directly (which is the better solution) then it becomes even easier.
UserProfileData.date = ((DateTime)row["DOB"]).ToString("MM/DD/YYYY");
Convert your string to DateTime type and use Date property, something like yourDate.Date
You can use DateTime.Parse or DateTime.ParseExact for parsing string to DateTime
It also make sense to return DateTime type directly from database so you do not need any conversions...
This question already has answers here:
Primefaces Calendar component & date conversions
(2 answers)
Closed 5 months ago.
I am developing a JSF and primefaces application. In my some.xhtml file where I put p: calendar tag and I want to set its default value as current date (format e.g:01-09-2014). I have written private Date startDate; in backing bean.
<p:calendar value="#{bean.startDate}">
When I don't write below code and just assign
private Date startDate = Calendar.getInstance(); it gives me default value(MON Sep 1 00:00:00 EST 2014) but not in proper format (01-09-2014) which I really want. So my question is that how to set date in custom format (01-09-2014) and assign it to Date object private Date startDate = this.defaultDate(); (see below:) to get default value in clientside
with this(01-09-2014) format.
I am using jdk 1.7.
private Date startDate = this.defaultDate();
private Date defaultDate() {
Date defaultDate = null;
try {
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
defaultDate = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return defaultDate;
}
You are parsing the date correctly (kind of, see below) with the DateFormatter but not formatting it again for display:-
sdf.format(defaultDate);
That is to say, when you output it:-
System.out.println(sdf.format(defaultDate));
A Date object only holds an actual date, not information on how it should be formatted or which pieces of information contained in the Date to display (seconds, minutes, hours, day of week, day of month, month, year). Hence the use of DateFormatter to select only the parts you wish to display, in the order you wish to display them.
Additionally
When you are originally getting the date:-
defaultDate = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
You are getting a Date object (with Calendar.getInstance().getTime()), converting it to a String using your DateFormatter (with sdf.format()) then using your DateFormatter to convert it back to a Date (with sdf.parse()) when you could just do:-
Date defaultDate = Calendar.getInstance().getTime();
you can use
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String displayDate = dateFormat.format(new Date());
this will give you formatted date in the format dd-MM-yyyy as string
Using J2ME, netbeans 7.2, Developing a mobile app..
I have converted the Datefield value to a String and Now want to put it back to a Datefield. To do this I need to convert the String back to Datefield, I am using the following code but its not happening.
long myFileTime = dateField.getDate().getTime(); // getting current/set date from the datefield into long
String date = String.valueOf(myFileTime); // converting it to a String to put it back into a different datefield
Date updatedate= stringToDate(date); // passing the string 'date' to the Method stringToDate() to convert it back to date.
dateField1.setDate(updatedate); // updating the date into the new datefield1
public Date stringToDate(String s)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));
return c.getTime();
}
Since you have mentioned that you have the long myFileTime around, you should be able to use:
Date updatedate=new Date(myFileTime);
To convert back to your date. If only your String is available, you should modify your function to this:
public Date stringToDate(String s){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));
return c.getTime();
}
Note the changed indexes.
In Java SE, you should be able to use the following line, instead of setting each fields separately:
c.setTimeInMillis(Long.parseLong(s));
Since in s you have the dateField.getDate().getTime() that is equal to myFileTime, the number of seconds starting with January 1, 1970, based on your provided code.
Your stringToDate should work only if your string will have the following format: ddMMyyyy. Also note that in this case you should use a SimpleDateFormat to parse, something like:
Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);
SPListItem.GetFormattedValue seems to have a strange behavior for DateTime fields.
It retrieves the DateTime value through SPListItem's indexer which according to this MSDN article returns local time.
Here's a snippet from Reflector
public string GetFormattedValue(string fieldName)
{
SPField field = this.Fields.GetField(fieldName);
if (field != null)
{
return field.GetFieldValueAsHtml(this[fieldName]);
}
return null;
}
So it uses SPListItem's indexer to retrieve the value and than SPFields.GetFieldValueAsHtml to format the value. GetFieldValueAsHtml seems to assume the date is in UTC and convert it to local time no matter what kind it is. (Reflector shows that it uses GetFieldValueAsText which uses value.ToString() but for some reason it assumes the time to be UTC.)
The end result is that the string representation on a time field obtained trough listItem.GetFormattedValue() (at least in my case) is incorrect, being local time + (local time - UTC).
Have anybody encountered the same issue with SPListItem.GetFormattedValue() and what was your workaround?
Converting the date back to universal time before calling GetFieldValueAsHtml works just fine.
DateTime localTime = (DateTime)item["DueDate"];
// this is local time but if you do localDateTime.Kind it returns Unspecified
// treats the date as universal time..
// let's give it the universal time :)
DateTime universalTime = SPContext.Current.Web
.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctFormattedValue =
item.Fields["DueDate"].GetFieldValueAsHtml(universalTime);
I have had a recognised bug with the date conversion from UTC in SharePoint. It was fixed in SP1.