最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I remove year from my date with moment.js? - Stack Overflow

programmeradmin4浏览0评论

I am using MomentJS to display a date.

moment(startDate).format('lll')

This will display: Jan 29 2014 03:04

But I would like to remove the year which is "2014" how can I do that?

Here is the link to the momentjs

MomentJS

I am using MomentJS to display a date.

moment(startDate).format('lll')

This will display: Jan 29 2014 03:04

But I would like to remove the year which is "2014" how can I do that?

Here is the link to the momentjs

MomentJS

Share Improve this question edited Feb 7, 2020 at 20:58 Poliakoff 1,6822 gold badges22 silver badges40 bronze badges asked Jan 29, 2014 at 11:52 ObsivusObsivus 8,35913 gold badges56 silver badges98 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

I haven't used that library before, but try this:

moment(StartDate, "MMM DD HH:mm");

At least this would work in .NET.

What you want is:

moment(startdate).format('MMM DD h:mm A');

See a javascript example:

http://jsfiddle/remus/rLjQx/

Following worked for me (ES2022 in Angular 17) -

// set locale
moment.locale(locale); // where locale is any valid supported string e.g. 'en', 'es', 'pt', 'fr', etc.

//check that not past year
const now = moment(new Date().toJSON());
const blogDate = moment(v.date); // where v.date is your UTC date input e.g. '2024-02-01T13:36:47.615Z'
const pastYear = now.year() > blogDate.year();
const differenceDays = now.diff(blogDate, 'days');

const date =
differenceDays > 7
? blogDate.format(pastYear ? 'll' : 'MMM DD')
: blogDate.startOf('day').fromNow();
console.log('date', date);

See it live here - https://gaurav.web-developer.in/en/blog

发布评论

评论列表(0)

  1. 暂无评论