How can I manipulate params inserted into and Edit form? - html

I have an object with a Time attribute. When the attribute is inserted into my Edit form it appears like:
2018-06-06 12:00:00 -0400
Is it possible to instead have the form show:
12:00 PM

Yes you can use strftime as following
created_at = Time.now
created_at.strftime("%I:%M %p")
which will produce out put like below based on your time
"03:23 PM"

Use strftime: strftime doc
datetime_var.strftime("%I:%M %p")

Related

Angular - using a date pipe with ngModel

I am getting the following error in my console - "Error: InvalidPipeArgument: 'Unable to convert "[object Object]" into a date' for pipe 'DatePipe'".
I have a calendar input, which should allow the user to select a date and then pass on that date in a certain format, 'dd/MM/yyyy'. I want the date selected to show in the calendar input once they have selected a date.
I realise I cannot have two way binding on the [ngModel] if I have a pipe there so I'm using
(ngModelChange). If I remove the #createdByCutOffDate="ngModel" then the error is removed but I cannot see the selected date show in the calendar input.
I also tried the updateCreatedByCutOffDate() method taking a date type or string.
this.createdByCutOffDate is in the following format - 'Thu Feb 17 2022 00:00:00 GMT+0000 (Greenwich Mean Time)'
component.html
<input type="date"
id="createdByCutOffDate"
[ngModel]="createdByCutOffDate | date:'dd/MM/yyyy'"
#createdByCutOffDate="ngModel"
(ngModelChange)="updateCreatedByCutOffDate($event)" />
component.ts
createdByCutOffDate: Date;
updateCreatedByCutOffDate(date: string) {
this.createdByCutOffDate = new Date(date);
}
createdByCutOffDate is a Date object that has its methods and properties.
So to solve your problem, use "createdByCutOffDate.date | date:'dd/MM/yyyy'" instead of "createdByCutOffDate | date:'dd/MM/yyyy'"

how to change date format in mongodb

I have created mongodb collection using nodejs as below:
var mongoose = require('mongoose');
var TTTUsersSchema = new mongoose.Schema({
username: String,
password:String,
active: Boolean,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Users', TTTUsersSchema);
Since creating record the default date is stored as Tue Nov 13 2018
20:53:47 GMT+0000 (GMT Standard Time) so when I fetch this field then it
is displayed in HTML as above. I want to display it in DD/MM/YY HH:MM
format.
Where I need to change? In HTML UI level? or at Mondgodb collection level.
Please help.
Time stamps should always be stored in a consistent way, e.g. like ISODate() in MongoDB, and always be handled in the code in a consistent way, e.g. like Date object in Javascript.
Only when you
present a time stamp to a user as string, or
parse a time string from the user
then you do conversions.
Unfortunately JavaScript Date object is severely limited, so I would suggest to use a package like Moment.js which offers lots of formatting capabilities:
const moment = require('moment');
// date is a Date object you got, e.g. from MongoDB
const time = moment(date);
console.log(time.format("DD/MM/YY HH:mm"));
You need to format your date time at view layer.
one option is to format at your controller if you are using MVC or at the layer that produce view model.
the another option is to format at html using javascript. You may need to use lib like moment.js
Assume current day is Tue Nov 13 2018 20:53:47 GMT+0000
Use .toDateString()
var date = new Date();
console.log(date.toDateString());
It will display:
Tue Nov 13 2018

Angular Date time conversion

I'm trying to convert the time and date from an html5 input to our countries format.
I'm trying to convert this: 2018-05-23 03:00
To this: 23-05-2018 03:00
I'm using angulars DatePipe like this:
var newDateBegin = this.dateFilter.transform(this.newDateBeginString.replace("T", " "), "dd-MM-yyyy HH:MM");
I have to replace the T in the string because for some reason the HTML5 input outputs it like this: 2018-05-23T03:00
But after converting it using: 2018-05-23 03:00, i get the following result:
2018-05-23 03:05.
No matter what time I put in there it always ends with xx:05. Am I missing something?
this is how i create the DatePipe:
private dateFilter: DatePipe = new DatePipe("nl-NL");
Isn't HH:MM the problem? M (uppercase) gets the month. Try to change it.
Month - MM - Numeric: 2 digits + zero padded
Use mm, as explained here
"dd-MM-yyyy HH:mm"

How to have a default value for datetime_field_tag in Rails

I am trying to have a default value for datetime_field_tag in my view:
=form_tag form_path, method: :get do
=label_tag :sign_up_at, 'Sign up before:'
=date_field_tag :sign_up_at, (params[:sign_up_at] ||
Time.zone.today.strftime("%Y-%m-%d"))
=label_tag :last_seen_at, 'Last seen after:'
=datetime_field_tag :last_seen_at, (params[:last_seen_at] ||
Time.zone.now.strftime("%d-%m-%YT%H:%M"))
=submit_tag 'Filter', class: 'btn-info'
This results in having a value tag in HTML:
<input type="datetime-local" name="last_seen_at" id="last_seen_at" value="22-05-2017T16:34">
My guess is, I am having some kind of a problem with parsing Time. Can anyone help, please? Thanks!
Updated according to #deephak's answer.
Just to supplement the other answer here: the datetime_field_tag helper is sensitive to the format of the datetime given to it. If, for example, the day, month, and year are in the wrong order, the value won't be displayed in the form input (and if you've got the developer console open, a warning message will be displayed).
It may depend on your locale, but the OP's "%d-%m-%YT%H:%M" format string has the year and day in reverse. "%Y-%m-%dT%H:%M" seemed to fix it in my trials.
You are using today which will give you just the date not time.
So, when you try to fetch time out of date rails will return 00:00
Example:
Time.zone.today
#=> Mon, 22 May 2017
Time.zone.now
#=> Mon, 22 May 2017 09:34:57 EDT -04:00
To fix this, change
Time.zone.today.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T00:00"
to
Time.zone.now.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"
# OR
Time.current.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"
The answers above are correct, but if you would like to refactor this, you can setup a timeformat initializer.
config/time_formats.rb
Time::DATE_FORMATS[:datetime_field] = '%Y-%m-%dT%H:%M'
Then when setting the value you can use:
Time.zone.today.to_s(:datetime_field)
Remember to restart your rails s after changing the initializer.
You can try
date_field_tag(:start_date, value = nil, options = {})
or if you want to submit onchange πŸ‘‡
date_select(:start_date, value = nil, options = {},onchange: "this.form.submit();")
If you want to try like rails, do like below.
<%= datetime_field_tag 'start_at_update', Time.zone.now.strftime("%Y-%m-%dT00:00:00"), placeholder: 'ν™˜μœ¨ κΈ°κ°„(μ‹œμž‘)', class: 'form-control w-50 d-inline-block h-auto w-auto ml-2 value-start-at_update', step: '1' %>

HandlebarsJS date format

I have a JSON file which returns multiple items with different values for concert dates.
So this value returns:
date: "2014-11-27"
and in my HandlebarsJS-template I have:
<p>{{date}}</p>
but I want to display the date like this:
NOV 27
by using HandlebarsJS and yes, I have MomentJS included too.
How can I achieve this?
You could register a Handlebars helper like this:
Handlebars.registerHelper('formatDate', function(dateString) {
return new Handlebars.SafeString(
moment(dateString).format("MMM D").toUpperCase()
);
});
After registering you have to change your template:
<p>{{formatDate date}}</p>
JSFiddle