I have two fields in my database for storing starttime and endtime. they are of datetime. I pick time from them using tostring("hh:mm tt"). Now I want to update only the time part of the date. I have dropdownlist to select hour and minutes and AM/PM. How can I update the time of date stored in sql server using Entity framework / LINQ in MVC3 application.
Please suggest
actually you don't really need to touch the linq part of this, basically what you want to do is just to convert the string back to a datetime object and just manipulate the datetime object by either creating a new datetime object or add/minus mm/dd/yy hours or minutes.
You must always update the whole datetime - it means you must build a new DateTime in your application and use correct Date part and defined Time part.
create new DateTime Object
DateTime mydate = New DateTime(2011, 6, 1, 12, 30, 0);
or
DateTime mydate = DateTime.Parse("2011-06-1 12:30:00 PM");
Related
I'm trying to add 2 DateTime fields which are stored as strings in Azure. The first datetime is supposed to get the current date and time now and this stores the date time as 12/02/2019 09:01 correctly, but currently the second field is supposed to get the current date and time and add 30 minutes but it is storing is like only the date eg 12/02/2019. This is my current code:
symptomFeedback.DateTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
symptomFeedback.Datetimelimit = DateTime.Now.AddMinutes(30).ToString("dd/MM/yyyy HH:mm");
From a java.util.Date( a timestamp), how can I get the hour of day?
In joda.time I use getHourOfDay().
There are multiple solutions for this. If you wish to use the Java 8 classes from java.time the following you need to covert a Date to one of the DateTime classes. The following can be used to convert a Date to a ZonedDateTime where you then can get the hour:
Date date = new Date();
// Convert to java 8 ZonedDateTime
Date date = new Date();
final ZonedDateTime dateTime = date.toInstant()
.atZone(ZoneId.systemDefault());
// Get the hour
int hour = dateTime.getHour();
Quite verbose as you have noticed but the simple reason for this is that a Date is sort of an Instant
Despite its name, java.util.Date represents an instant on the time-line, not a "date". The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC).
Another approach is simply to get the field from a Calendar instance.
final Calendar instance = Calendar.getInstance();
instance.setTime(date);
final int hourOfDay = instance.get(Calendar.HOUR_OF_DAY);
How can I concatenate the values of two datetimepicker?
DateTimePicker1 - this is for the the DATE mm/dd/yyyy
DateTimePicker2 - this is for the the TIME hh:mm:ss
I want a value of ... mm/dd/yyy hh:mm:ss in one variable, how can I do that?
One easy way would be
DateTime dt = new DateTime(dtp1.Year, dtp1.Month, dtp1.Day, dtp2.Hour, dtp2.Minute, dtp2.Second);
Please check the order of the parameters, I am writing from memory.
Here dtp1 is a DateTime object from the first datetime picker.
I have Oracle database with Date column in it and I want to retrieve it from recordset using Calendar since I want to compare it with today's date which is built using Calendar.
Not really sure what you meant with retrieving the date "using Calendar", but if you are using Java, and want to read a date from database and compare it with today's date, it goes like this:
java.sql.Date dbDate = resultset.getDate("dateField");
java.util.Date date = new Date(dbDate.getTime());
Calendar calendar = Calendar.getInstance();
java.util.Date today = calendar.getTime(); // "today = new Date();" would work just as well
if (date.before(today)) {
// do something
} else if (date.after(today)) {
// do something else
}
I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)