How to format a date in moment js - momentjs

How to format 2019-07-29T05:55:00 to 29 Jul 2019 (Mon) in moment js ? What should be the formatdate string ? 'DD MMM YYYY (ddd) is not accepted by moment js

I'm not allowed to comment yet, but I when I saw your question I was sure your format was correct.
var now = moment().format('DD MMM YYYY (ddd)');
Tried it out in JS fiddle and it works.

It should be working fine, the correct syntax:
var test = moment().format('DD MMM YYYY (ddd)');

var myDate = moment('2019-07-29T05:55:00').format('DD MMM YYYY (ddd)');
result.innerHTML = myDate ;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.js"></script>
<pre id="result"></pre>

Related

Issue with using moment.js

I was creating project in Angular and to format date I decided to use moment.js. But the problem is from the backend I get such format "2020-02-06" thus I decided to use 'MMM DD, YYYY' and I want "2020-02-06" to look like Feb 6, 2020 with this format MMM DD, YYYY. So, to achieve that I have this code
moment(
response.data.projectCreatedDate //this contains "2020-02-06"
).format("MMM DD, YYYY");
BUT the problem is instead of getting Feb 6, 2020 I get 13 May, 2020 which is today's date. Pls can you help, what am I doing wrong?
Moment with empty argument returns current date, and format on top of it returns current formatted date .Your date from server might be undefined, Hence you are the getting current date formatted.
You can also pass custom date for formatting too!
//Formatting an input date
var str = "2020-02-06";
console.log(moment(str, "YYYY-MM-DD").format("MMM D,YYYY"));
//Formatting current date
console.log('Current Date: ', moment().format("MMM D,YYYY"));
//Formatting array of dates
var arr = ["2020-02-06", "2020-01-13"];
var res = arr.map(date => {
return moment(date, "YYYY-MM-DD").format("MMM D,YYYY");
});
console.log(res);
<script src="https://momentjs.com/downloads/moment.js"></script>
Nothing is wrong with moment here, you are getting an empty response inside response.data.projectCreatedDate from your server.
moment("2020-02-06").format("MMM DD, YYYY") //Feb 06, 2020
moment().format("MMM DD, YYYY") //todays date.
Thanks.

Locale time on IONIC2 Datetime picker

I'm working with IONIC2, Angular2 and Typescript. I have an Datetime working as follows:
page.html
<ion-datetime displayFormat="DD MMMM YYYY" pickerFormat="DD MMMM YYYY" [(ngModel)]="date"></ion-datetime>
page.ts
date: string = new Date().toISOString();
The ion-datetime field shows time with an hour less, how can I display date on Datetime picker considering the timezone?
Reading this answer I solve my problem. Finally I use:
moment(new Date().toISOString()).locale('es').format();
Thanks to sebaferreras
the simplest is to remove timezone offset in milliseconds:
date = new Date();
myDate: String = new Date(this.date.getTime() -
this.date.getTimezoneOffset()*60000).toISOString();

Pikaday format not working

If I have a datetime string that come from my database e.g. 2016-02-07 00:00:00
how can I get my input field to display this using format 'DD MMM YYYY'
I have moment.js defined and the following pikday instance but the default set value doesn't display as I want it. Instead the input field displays it as 20 Jan 0000. How can you get pikaday to display any valid datetime string in the format that you want?
<input id="start_dt" type="text" value="2016-02-07 00:00:00" name="start_dt">
var start_dt = new Pikaday(
{
field: $('#start_dt')[0],
format: "DD MMM YYYY"
}
You will have to set the default value to the one that you have.
First up get the date value from the input.
var date = $( '#start_dt' ).val();
And then,
var picker = new Pikaday({
field: document.getElementById('datepicker'),
defaultDate : moment(date).format("DD MMM YYYYY"),
format: 'DD MMM YYYY',
});

set culture information for custom date format

I am facing a problem in date format. I am converting a string date(dd/MM/yyyy) to datetime, using convert.toDateTime(). It works fine on my local machine but causes problem when I run from server. So to set uniformality, I tried to set culture info for (yyyy-MM-dd HH:mm:ss) format, but couldn't set as .net shows an error.
I tried like this.
CultureInfo DateInfo = new CultureInfo("yyyy-MM-dd HH:mm:ss");
How can I set the culture info for this format?
Try to use like this...
string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
string format = "ddd dd MMM h:mm tt yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Response.Write(dateTime);
hope this may helpful...

Joda Time formats time at 24:xx:xx UTC should be 0:xx:xx UTC

I'm converting from a local time zone to UTC so when we convert
2010-01-03T11:15:58.840+11:00 => Sun, 03 Jan 2010 24:15:58 UTC
This is technically correct but I'm having problems with the 24 hour formatting as it does. I have some BlackBerry J2ME code which is having problems parsing this date-time String using HttpDateParser.
new Long(HttpDateParser.parse("Sun, 03 Jan 2010 24:15:58 UTC")
Shouldn't this really be "Sun, 03 Jan 2010 0:15:58 UTC"? If I pass in this new date String it will parse just fine. I'd rather not do a nasty "search and replace", but fix the problem at the server.
Question: Is it possible to stop Joda from displaying times as "24:xx:xx" and instead format as "0:xx:xx"?
Edit: I'm formatting the output date as
public static final SimpleDateFormat DATE_FMT =
new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz");
Try this format (HH instead of kk):
public static final SimpleDateFormat DATE_FMT =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
That information is in the SimpleDateFormat JavaDoc.
Joda’s formatter has similar pattern format.

Resources