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
Related
is it possible to specify a date in the JSON structure, like for example in PHP (https://www.php.net/manual/de/function.date.php):
$my_date = date("m.d.Y");
What I mean, here is an example of the JSON file:
{
"dates_set_1":"test-1",
"my_date":date("m.d.Y")
}
Is it possible to specify a date like this?
And if so, how?
Yes you can do it. Like this
date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
I'm new in web development.
my problem is:
how to disaply in html a date in simple format dd/mm/yy
instead of Fri Sep 01 2000 00:00:00 GMT+0300 (Israel Daylight Time)
I'm trying to create a crud api using mongoose.
I have a schema model,
one of the fileds is a filed of type date:
joinDate: {type: Date, required: true}
in the controller:
post- program.joinDate= req.body.joinDate;
get-
Program.find((err, docs) => {
res.render("program/list", {
list: docs
});
});
in the view: <td>{{this.joinDate}}</td>
My question: The easy and fast way to convert the long format to short and cleraly format.
I saw some solutions but I think they are too complex and outdated.
If you have any idea for me I would be happy:)
thank's you!
I often use mongoose virtual + moments for this.
eventSchema.virtual('joinDate_formatted').get(function () {
return moment(joinDate).calendar({
sameDay: 'h:mm A',
nextDay: '[Tomorrow] h:mm A',
nextWeek: 'dddd',
lastDay: '[Yesterday] h:mm A',
lastWeek: '[Last] dddd',
sameElse: 'DD/MM/YYYY h:mm A'
}); //Above shows in calendar format but u can do whatever u want in moment
});
When u do any find on the collection, you will get a joinDate_formatted which u can use on your front end.
My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.
Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.
If you're using Eloquent Models you can do it very easily:
1. Add the columns to the $dates property in your model class:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutated to Carbon instances.
2. In your view files you can use the format method that Carbon offers like so:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
I have used the PHP solution without Carbon:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
date('Y-m-d H:i:s', strtotime($request->date_begin))
protected $casts = [
'inicio' => 'datetime:Y-m-d\TH:i'
,'entrega' => 'datetime:Y-m-d\TH:i'
];
you could use it for the datetime-local field. greed you. like me if work for you
// attributes that should be cast to native types
protected $casts = [
'email_verified_at' => 'datetime'
];
// get formatted datetime string for email_verified_at
public function getEmailVerifiedAttribute()
{
if ($this->email_verified_at) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
} else {
return null;
}
}
as Laravel document way.
I am using the Jira API, and need the start and end dates for a sprint.
The JSON data I get back is :
{"jodaTimeZoneId":"Europe/Berlin","sprints":[{"id":5,"start":"13082015044305","end":"27082015044305",...
Normally, json returns the date in milliseconds, and you need to deserialize that.
Now however, I can clearly see the date (13-08-2015 & 27-08-2015) followed by some other numbers I don't care about. Is there anyway Angular can get the correct format using | date? Or any other way I can use?
When I use {{13082015044305 | date:'dd-MM-yyyy'}} it returns 21-07-2384. The parsing date format is wrong. So change the format to recognized way.
So I used
input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
Used it in a custom filter.
app.filter('correctDateFormat', function() {
return function(input) {
return input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
};
});
Then
Display the date as
{{13082015044305 | correctDateFormat }}
I think you can use
{{ data | filter:options }}
where data is your json and date filter
{{'1388123412323' | date:'MM/dd/yyyy # h:mma'}}
an option like this.
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.