Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

javascript - Locale and specific date format with Moment.js

I am using Moment.js in my project and formatting dates as follows:

var locale = window.navigator.userLanguage || window.navigator.language;
moment.locale(locale);
someDate.format("L");

It works well but sometimes I need show a date without a year. I can't use something like someDate.format("MM/DD") because in some languages it should be someDate.format("DD/MM"). I need something like L,LL,LLL but without the year.

What can I do?

LTS : 'h:mm:ss A',
LT : 'h:mm A',
L : 'MM/DD/YYYY',
LL : 'MMMM D, YYYY',
LLL : 'MMMM D, YYYY LT',
LLLL : 'dddd, MMMM D, YYYY LT'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay. This is a little awful, but you knew it was going to be.

First, you can access the actual format string for (for instance) 'L':

var formatL = moment.localeData().longDateFormat('L');

Next, you can perform some surgery on it with judicious regex replacement:

var formatYearlessL = formatL.replace(/Y/g,'').replace(/^W|W$|WW/,'');

(Which is to say: Remove YYYY, plus the orphaned separator left by its removal)

Then you can use your new format string in a moment format call:

someDate.format(formatYearlessL);

This necessarily makes some assumptions:

  • The order of the month + day numeric format for a locale matches the order for the year + month + day format for that locale, with the year removed.
  • The short form uses separators only between month and day (no leading / trailing separators).
  • The separator for a short numeric date format is always non-alphanumeric.
  • The format consists of numeric elements and separators, rather than a sentence-form format with articles (see RGPT's comment below about Spanish and Portugese, which will also apply to long formats in some other languages).

On a quick review of locale/*.js, these assumptions hold true for every locale file I examined, but there may be some locales that violate them. (ETA: a comment below points out that a German short date format violates the second assumption)

As an additional important caveat, this is likely to be fragile. It is entirely possible that a future version of moment.js will change the location of the data currently in longDateFormat...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...