composer require alexandermatveev/moment-bundle
config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
AlexanderMatveev\MomentBundle\AlexanderMatveevMomentBundle::class => ['all' => true],
php bin/console assets:install public --symlink
<script src="{{ asset('bundles/alexandermatveevmoment/moment.min.js') }}"></script>
Use Moment.js directly in JavaScript for client-side date handling:
// Example: Format a date in Twig, then manipulate it in JS
const date = moment("{{ some_date|date('YYYY-MM-DD') }}");
date.format('LLLL'); // "Monday, January 1, 2023 12:00 PM"
<script>
const userCreatedAt = moment("{{ user.createdAt|date('x') }}");
userCreatedAt.fromNow(); // "2 days ago"
</script>
date() Filter in Twig for consistency:
{{ event.startTime|date('x') }} {# Unix timestamp for Moment #}
moment-with-locales.min.js:
<script src="{{ asset('bundles/alexandermatveevmoment/moment-with-locales.min.js') }}"></script>
moment.locale('fr'); // French translations
webpack or encore to bundle Moment.js with your app.assets:install for symlinked assets (faster updates).bootstrap-datetimepicker:
$('#datetimepicker').datetimepicker({
useCurrent: false,
format: 'YYYY-MM-DD HH:mm'
});
moment-timezone (separate npm package) if needed.Outdated Version:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js).npm install moment@latest) and bundling manually.Asset Path Changes:
bundles/alexandermatveevmoment/ path breaks (e.g., Symfony 4+), check:
php bin/console assets:install public --symlink --verbose
Locale Conflicts:
moment is not a function).php bin/console assets:debug
Uncaught ReferenceError: moment is not defined (missing script tag or wrong path).Custom Builds:
moment-with-locales.min.js for minimal footprint (only include needed locales).npm run build (if using npm).Symfony 4+ Compatibility:
Resources/public/ in your project to use public/bundles/... paths.Testing:
// In your test JS file
window.moment = require('moment');
How can I help you explore Laravel packages today?