This is one of the models from my schema:
model playlist {
id Int #id #default(autoincrement())
list String #db.VarChar(34)
duration DateTime? #db.Time(0)
}
And I would like to add a time value when I am creating a new record. But it seems that I can only add value with the DateTime type.
I tried just passing a String but that doesnt work and I get and error.
This is the error:
Argument duration: Got invalid value '00:01:29' on prisma.createOneplaylist. Provided String, expected DateTime or Null.
Is there a way to only add the time or am I forced to use DateTime format?
You would always need to send a Date object in this case. For e.g. this is how I would set the time using date-fns.
import set from 'date-fns/set'
await prisma.playlist.create({
data: {
list: 'list',
duration: set(new Date(), { hours: 1, minutes: 10 }),
},
})
A similar parsing mechanism can be used when you retrieve the time.
Related
Let's say 01/01/2022 is selected from the input, and my dates array is something like below
['31/12/2021', '01/11/2021', '02/01/2022'...]
then my form validation must fire and gives a validation error since '31/12/2021' and '02/01/2022' are consecutive dates for the selected date.
I'm using the reactive forms technique here.
You can create a Map for dates that are previous to every date in your array and another for the following dates and compare if both have the same input date.
const previous = {[key: string]: string}
previous = { '02/01/2022' : '01/01/2022'}
const following = {[key: string]: string}
following = { '31/12/2021' : '01/01/2022'}
and do something like
if(previous['02/01/2022'] === following['31/12/2021']){
//fire error here
}
I have the following code that does not update the Database "date" column when the field type in MYSQL is DATE.
When I change the column type to VARCHAR the date gets updated. I need to keep the column as DATE so that I can perform calculations such as determine Age.
I think the question to ask is how to get the value from DateInput and update the date column? Looks like I may need some conversion before passing value to database but I am not sure. Appreciate any help.
import React, { Component } from 'react'
...
import DateInput from '../others/input/datePicker'
...
class EditProfile extends Component {
state = {
...
...
changeDOB = () => {
this.setState({ age: document.getElementById("datePicker").value })
}
...
render() {
let {
...
...
} = this.state
<DateInput
type="date"
value={age}
onChange={this.changeDOB}
placeholder="Birth Date"
className="my2"
id="datePicker"
/>
...
The date value that comes from html input date field is not compatible with the date in MYSQL. The MYSQL documentation suggests that the date format is UTC. You have to perform some updates to the date before passing it onto the table.
VARCHAR works for you because it does not need to fulfill any format requirements to be updated in the database.
I have to define a date in my Student Object,like that:
var Students = new List<Student>
{
new Student{ FirstName="Student ",dateBirth=DateTime.Today }
};
Students.ForEach(s =>
{
s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
context.Students.Add(s);
context.SaveChanges();
});
but I get this format when I run my project:
[{ FirstName: "Student ", dateBirth: "/Date(1457132400000)/" }]
the problem is that the date format is added exactly to the DataBase Student Table (like yyyy-MM-dd hh:mm:ss). I work with xampp MySQL
any idea please how can I solve this problem,and display the date correctly?
Try formatting the date time like this:
new Student{FirstName="Student ",dateBirth=DateTime.ToString("MMMM dd, yyyy")}
You can read up on the details here.
Custom Date and Time Strings
I''m using EF to query the database using anonymous type.
here the code I use for EF
public JsonResult OverdueEventsCustom()
{
var eventCustomOverdue = _eventCustomRepository.FindOverdueEventsCustom();
return Json(eventCustomOverdue, JsonRequestBehavior.AllowGet);
}
public IQueryable<dynamic> FindOverdueEventsCustom()
{
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateTomorrow = dateTimeNow.Date.AddDays(1);
return db.EventCustoms.Where(x => x.DateTimeStart < dateTomorrow)
.Select(y => new { y.EventId, y.EventTitle, y.DateTimeStart});
}
Inspecting using the debugger I see the properties is in this format
Date = {16/08/2012 00:00:00}
The resultfor the JSON is
[{
"EventId": 1,
"EventTitle": "Homework Math",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 4,
"EventTitle": "Homework help with Annie",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 6,
"EventTitle": "Physic laboratory",
"DateTimeStart": "\/Date(1345108269310)\/"
}]
I need the the json in this format
"DateTimeStart": "(16/08/2012)"
Any idea what i'm doing wrong here? thanks for your help
Related articles
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
How do I format a Microsoft JSON date?
"\/Date(1345108269310)\/" is the correct way to pass a Date to javascript. The way I see it, you have two options here:
If you do not explicitly need the value as a date, you could just pass a string to the JSON variable, containing the pretty-printed date.
Something along the lines of:
DateTimeStart: String.Format("{0: dd-MM-yyyy}", myDate)
If you will still need to use the variable a a date in javascript (for calculations for example), the most consice and readably way would be to create a javascript function that converts said date into the pretty-printed string you want (I don't know if such a function already exists. It isn't too hard to create though:
function prettyDate(date) {
return date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear();
}
I would suggest passing it along as a string from you code behind, as it is more readable. But that only works if you do not need to use the date except for displaying.
I use jqGrid and my grid definition is like that:
...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date',
formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}],
...
When I debug my coming data one of the date value (it is Number) is as follows:
1322550786997
Grid shows it like that:
29-Nov-2011
Everything is OK till this point. However when I want to sort my date column it doesn't change anything.
Any ideas?
The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.
See the demo:
UPDATED: You can also use the following custom formatter as a workaround
formatter: function (cellval, opts) {
var date = new Date(cellval);
opts = $.extend({}, $.jgrid.formatter.date, opts);
return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}
It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.