I have a problem in my DateTextField in Wicket. I want to set it by default to nothing, null, so date can be choosed after someone picks it. But by default it sets value to current day. How to "clear" it so nothing is in this datetextfield? Here is my Datefield code:
DateField date_insert_date_from = new DateField("insert_date_from", new PropertyModel(this, "date")) {
/**
* Format date to yyyy-MM-dd pattern.
*/
#Override
protected DateTextField newDateTextField(String id, PropertyModel dateFieldModel) {
return DateTextField.forDatePattern(id, dateFieldModel, "yyyy-MM-dd");
}
};
form.add(date_insert_date_from);
As #kan says, you need to make sure the modelobject for the datefield is null.
Simple way to do so is:
DateField date_insert_date_from = new DateField("insert_date_from", new Model<Date>(null));
Now, if anyone enters a date into the datefield and submits the form, the Model will contain the chosen date and you can retrieve it by writing:
Date chosenDate = date_insert_date_from.getModelObject();
If you want to use a propertymodel, as you do, you need to make sure the object on which the propertymodel acts (this in your case) has a field capable of holding a date and getter/setter methods for that field.
In your case, this.date should be initialized with null and this should have
public Date getDate()
and
public void setDate(Date date)
methods.
Related
I have in my table a timestamps with created_at and updated_at
$table->timestamps();
But I want to add another column that calculates the difference between created_at and updated_at in days
Should I do that in SQL or in my controller?
You can do that from within your Eloquent model.
Let's assume your model has the name User.
You can create a computed field by defining an Accessor.
class User extends Model
{
$dates = [
'updated_at',
'created_at'
];
$appends = ['diffInDays'];
public function getDiffInDaysAttribute()
{
if (!empty($this->created_at) && !empty($this->updated_at)) {
return $this->updated_at->diffInDays($this->created_at);
}
}
}
Some explanation
By adding created_at and updated_at to the $dates array, Laravel automatically casts your date values to Carbon.
Now, if you do something like $user->created_at, you don't get the string, but a Carbon instance of that date. This allows you to make some nice date calculations, like the one above.
By adding an Accessor with the getDiffInDaysAttribute function, you can call the days difference via $user->diffInDays like a normal attribute, although it is not on the model.
But if you would now do something like $user->toArray(), the diffInDays attribute will not be available.
To always add the difference in days when you retrieve User data, you can add the field to the $appends array.
That way, the field will always be returned when you retrieve User data via Eloquent.
To have it auto save this value on every update to that model, then you can put this in the model.
public static function boot()
{
parent::boot();
static::updating(function($model) {
$diffInDays = $model->updated_at->diffInDays($model->created_at);
$model->timestamp_difference = $diffInDays;
});
}
timestamp_difference is the name of the DB column that I used, this can be whatever you want it to be.
Use Carbon for count date difference in days.
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $created_at);
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $updated_at);
$diff_in_days = $to->diffInDays($from);
WatchFromDate().sendKeys("2018-07-03");
// public WebElement WatchFromDate()
{
return driver.findElement(fromDate);
}
By fromDate= By.id("from");
the steps to send the system date in selenium could obtain the date with a specific format and then send this variable. Something like that:
public void WatchFromDate(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
driver.findElement(fromDate).sendKeys(dtf.format(localDate));
}
The dtf variable defines the format for the date (i.e. 2018/07/13)
The localDate variable obtain the current date, but this date has too much precision.
The dtf.format(localDate) sentence adjust the current date obtained in the localDate, to the defined format in dtf variable.
Now that NHibernate 5 is out, I was wondering what's the recommended approach to load dates as local dates when they're persisted as datetime2 in a SQL Server 2016 database. Until now, dates were interpreted as local (so, the properties ended up with the correct values), but now, the behavior has changed and I'm unable to use the LocalDateTime.
Unfortunately, my donmain's behavior relies in getting those dates automatically loaded as local dates...
Any pointers on how to solve this?
Thanks,
Luis
Since I didn't found a way to disable this behavior, I've ended up creating a new type that will simply adjust the format of the data (considering always that it was saved in local format):
public class LocalDateTimeTypeNoThrow : LocalDateTimeType {
public LocalDateTimeTypeNoThrow() {
}
public LocalDateTimeTypeNoThrow(DateTimeSqlType sqlType) : base(sqlType) {
}
public override string Name => "LocalDateTimeNoThrow";
public override void Set(DbCommand st, object value, int index, ISessionImplementor session) {
var dateValue = (DateTime) value;
//removed throwing from here
st.Parameters[index].Value = AdjustDateTime(dateValue);
}
}
If there's a better way, please let me know.
Luis
I have a SQL-Azure database created with Entity Framework 6.1, Code-First.
The "datetime" field in my 'EmazeEvents' table was created like this:
datetime = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()")
and defined like this in the code:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Index]
public DateTime datetime { get; set; }
I understand this means that in case this field is omitted in insertion, it will get by default the insertion date, which indeed it does.
However, I am having trouble inserting rows that set this field. Although I set the value of the appropriate variable, it still writes to the database the default date.
Some code extractions:
EmazeEvents is defined like this:
public class EmazeEvents:DbContext
{
public EmazeEvents()
: base("EmazeEvent")
{ }
public DbSet<EmazeEvent> events { get; set; }
}
}
What I do is:
context = new EmazeEvents();
EmazeEvent e = new EmazeEvent();
// e.datetime does get the correct date
e.datetime = DateTime.ParseExact("2014-05-31T00:00:06.8900000", "O", CultureInfo.InvariantCulture);
context.events.Add(e);
context.SaveChanges();
The record written to the database has the current date-time, ignoring the one in e.datetime.
I found out that the problem was with the definition of the 'datetime' field. When I removed the:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
It started letting me write other values than the default.
I created a calendar event with all day selected (10/24/2013) . Then i tried to pull the e.EventStart and e.EventEnd. My expected result was EventStart =10/24/2013 12.00.00 am and
EventEnd= 10/25/2013 12.00.00 am. But what am getting is EventStart =10/24/2013 12.00.00 am and EventEnd= 10/24/2013 12.00.00 am.
The same works fine when I try with e.EventStartUtc and e.EventEndUtc. But I dont want the utc format as I am trying to pull out the ektron time for the users.
If you use the Framework API, the WebEventData class has a property called IsAllDay. You could use this to trigger the display change. For example, you might not want to display start/end times at all if it's an All Day event and just show the date.
If you do need to have particular start/end times for all day events, you can easily extend the Ektron WebEventData object with extension methods.
public static class WebEventExtensions
{
public static DateTime GetDisplayStartDate(this WebEventData webEvent)
{
if (!webEvent.IsAllDay)
return webEvent.EventStart;
return new DateTime(webEvent.EventStart.Year, webEvent.EventStart.Month, webEvent.EventStart.Day);
}
public static DateTime GetDisplayEndDate(this WebEventData webEvent)
{
if (!webEvent.IsAllDay)
return webEvent.EventEnd;
return new DateTime(webEvent.EventEnd.Year, webEvent.EventEnd.Month, webEvent.EventEnd.Day, 23, 59, 59);
}
}
Then those methods will appear on the object.
var eventManager = new WebEventManager();
WebEventData webEvent = eventManager.GetItem(730);
if (webEvent.IsAllDay)
{
// do all-day stuff...
}
var start = webEvent.GetDisplayStartDate();
var end = webEvent.GetDisplayEndDate();