Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Last active October 7, 2023 19:42
Show Gist options
  • Save AshKyd/8843581 to your computer and use it in GitHub Desktop.
Save AshKyd/8843581 to your computer and use it in GitHub Desktop.
A momentjs RFC2822 formatted date string.
var DATE_RFC2822 = "ddd, DD MMM YYYY HH:mm:ss ZZ";
moment().format(DATE_RFC2822);
@LeoBenoist
Copy link

In your client browser is not in english this will not work. It should be semothing like :

    var DATE_RFC2822 = "ddd, DD MMM YYYY HH:mm:ss ZZ";
    moment().lang("en").format(DATE_RFC2822); 

@Miges
Copy link

Miges commented Jul 22, 2015

Thanks mate - really nice.

@rastalamm
Copy link

Nice!

.lang() has been deprecated.

I received this error: Deprecation warning: moment().lang() is deprecated. Instead, use moment().localeData() to get the language configuration. Use moment().locale() to change languages.

You should use .locale("en") to set it to English

@redben
Copy link

redben commented May 19, 2016

Note that http headers MUST always be in GMT, so the correct way is

var DATE_RFC2822 = "ddd, DD MMM YYYY HH:mm:ss [GMT]";
moment.utc().format(DATE_RFC2822);

From the RFC

All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. For the purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal Time).

@alexturpin
Copy link

@redben This is useful, but I believe the original code was meant to be for RFC 2822 which can take a timezone.

@erose
Copy link

erose commented Aug 11, 2016

This was very useful, thank you!

@RavenHursT
Copy link

OMG.. Why oh why doesn't @moment put this in their docs?? https://gist.github.com/AshKyd/8843581#gistcomment-1781610

Thank you @redben !

@agustin-rr
Copy link

moment.utc().locale('en').format('ddd, DD MMM YYYY HH:mm:ss [GMT]');

@alanibrus
Copy link

As JavaScript's .toUTCString() outputs RFC 2822 formatted date, you can just use:
moment().toDate().toUTCString();

@Giayychan
Copy link

@alanibrus i think you got the simplest answer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment