Rails Add TimeStamp to DatePicker Date From Form - date

I'm using rails 5.2.4.4 and ruby 2.6.4
I'm using the JQuery DatePicker on a form. It is giving me a date in the format: mm/dd/YYYY. What I need to do is add a time-stamp to that so I can make the date look like 01/12/2021 23:23:59. I thought I could add the end_of_day method in a before_hook call, but rails does not like that:
# in model:
validates :end_date, :presence => true
before_save :set_end_date_timestamp
def set_end_date_timestamp
if self.end_date?
self.end_date.end_of_day!
end
end
That gives me an error: NoMethodError (undefined method 'end_of_day!' for Tue, 12 Jan 2021 00:00:00 EST -05:00:Time)
What am I doing wrong? And, what is the most railsie way to accomplish this?

Found this:
<script>
// make date format user friendly: mm/dd/yyyy
$("#course_swap_request_date_start_date").datepicker(
{ dateFormat: "mm/dd/yy 00:00:01" }
);
$("#course_swap_request_date_end_date").datepicker(
{ dateFormat: "mm/dd/yy 23:59:59" }
);
</script>
For my specific app, I need to set the start_date timestamp to 00:00:01 and the end_date timestamp to 23:59:59. I could not get beginning_of_day and end_of_day to work. Since DatePicker allows us to set the timestamp this way, it was an easy way to do this, albeit a bit of a cheap-hack.

Related

Converting from 'H:mm a' format back to DateTime can't be parsed

const date = DateTime.fromISO('2022-03-27T08:50').toFormat('H:mm a') // 08:50 AM
console.log(DateTime.fromISO(date))
If I attempt the above, in the console log I get this explanation in the 'invalid' field:
explanation: "the input "8:30 AM" can't be parsed as ISO 8601"
reason: "unparsable"
Is it not possible to revert the string back to a date?
You can parse back "8:30 AM" as DateTime using fromFormat:
Create a DateTime from an input string and format string. Defaults to en-US if no locale has been specified, regardless of the system's locale. For a table of tokens and their interpretations, see here.
but the information about year, month and day are lost and so the new DateTime will default to the current day.
Example:
const DateTime = luxon.DateTime;
const date = DateTime.fromISO('2022-03-27T08:50').toFormat('H:mm a') // 8:50 AM
console.log(DateTime.fromFormat(date, 'h:mm a').toISO())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>

Formik Yup date validation - accept European DD/MM/YYYY format

I am trying to use Yup with Formik for my user profile screen. The validation works fine but it expects the format of the date entered by the user to be in USA format MM/DD/YYYY rather than the application required European/UK standard format DD/MM/YYYY. Entering 31/12/1995 fails validation.
dateOfBirth: Yup.date()
.required("Date of Birth is required")
.max(dateToday, "Future date not allowed")
I have searched through the Yup docs and SO but I can't work out how to do this. Any ideas?
You can use the transform method to parse value.
Like:
startDate: Yup.date()
.transform(value => {
return value ? moment(value).toDate() : value;
})
.required("Date of Birth is required")
.max(dateToday, "Future date not allowed");
I had this same issue myself and resolved it using the example in the Yup README replacing MomentJS with date-fns which is what I use for date manipulation.
Value returns Invalid Date before you custom transform is applied so you must use the original value and context to check to see if you need to run the transform logic at all and if so run it on the value from the field and not the transformed value.
Yup transform docs and date example
import { parse } from 'date-fns';
[...]
date()
.transform((value, originalValue, context) => {
// check to see if the previous transform already parsed the date
if (context.isType(value)) return value;
// Date parsing failed in previous transform
// Parse the date as a euro formatted date string or returns Invalid Date
return parse(originalValue, 'dd/MM/yyyy', new Date());
})
This works perfectly for me and works for both US and UK date formats (you will still need to perform manipulation on the date if its in the us format as it will submit this value as valid)
If you ONLY want UK/Euro dates then just remove the context type check
.transform((value, originalValue) => parse(originalValue, 'dd/MM/yyyy', new Date()))

Change input Date format? Vuejs + mongoose

Problem: Change the Date input field from "mm/dd/yyyy" to "dd/mm/yyyy".
I already know how to change after i receive the date, but the problem is that when the client is typing the input is still receiving "mm/dd/yyyy".
My mongoose schema:
const schemaRegister = new mongoose.Schema({
date: Date,
});
My input area:
<b-form-input v-mask="'##/##/####'" v-model="date"></b-form-input>
My date formating (using momentsjs):
changeDateFormat() {
let fixedDate = moment(this.registers[i].date).format("L");
this.registers[i].date = fixedDate;
}
I am displaying the 'fixedDate' on the table, but it doesn't help a lot because when the client is typing he thinks the first 2 slots are the days (dd), but in reality they are the month (mm). As a solution i thought of using the Date as a String but then it would make the verification very difficult.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
just pass the parameters in the correct order, like this:
new Date(day, monthIndex, year);
I wasn't using the 'momentsjs' correctly, first i needed to parse the input date by using
let formatedDate = moment(this.date,"DD-MM-YYYY");
and then for displaying the date i should have used
let fixedDate = moment(this.registers.date).format("DD/MM/YYYY");

Save a birthDate via qraphql in postgres

I would like to save a birthdate from an (react) input (type = 'date'), send it via GraphQL to node backend and persist it in postgres in a date format.
Input in HTML: 09.07.2000
In GraphQL resolver: 2000-07-09T00:00:00.000Z
Date format in Postgres (original output in console): 09.07.2000
Well, that's what i expected. But now, if i request the same field:
Date format in Postgres (original output in console): 09.07.2000
Graphql response: 08.07.2000
HTML Input: 08.07.2000
If I change the scalar in graphQl schema to String, the following string returns: Fri Jul 09 2000 00:00:00 GMT+0200 (CEST)
Code
schema
scalar Date
type Child {
...
birthDate: Date
...
}
resolvers
const { GraphQLDate } = require('graphql-iso-date')
...
Date: GraphQLDate,
Problem
It looks like there is a problem in converting the date format from different timezones. If there is no timezone the scalar resolver guess a timezone. But this is a birthdate, it hast to be the same date on every timezone. How can I fix this? Do I have to use a string instead of date in prostgres?
Thanks for any support 🙏 I'm really lost
UPDATE
It looks like knex.js is the problem.
A normal SQL query responds the expected date. But a query with knex.js response a datetime.

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)