formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LLL');
};
It displays: "28 februari 2013 09:24"
But I would like to remove the time at the end. How can I do that?
I'm using Moment.js.
Sorry to jump in so late, but if you want to remove the time portion of a moment() rather than formatting it, then the code is:
.startOf('day')
Ref: http://momentjs.com/docs/#/manipulating/start-of/
Use format('LL')
Depending on what you're trying to do with it, format('LL') could do the trick. It produces something like this:
Moment().format('LL'); // => April 29, 2016
The correct way would be to specify the input as per your requirement which will give you more flexibility.
The present definition includes the following
LTS : 'h:mm:ss A',
LT : 'h:mm A',
L : 'MM/DD/YYYY',
LL : 'MMMM D, YYYY',
LLL : 'MMMM D, YYYY h:mm A',
LLLL : 'dddd, MMMM D, YYYY h:mm A'
You can use any of these or change the input passed into moment().format().
For example, for your case you can pass moment.utc(dateTime).format('MMMM D, YYYY').
Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted YYYY-MM-DD.
moment().format(moment.HTML5_FMT.DATE); // 2019-11-08
You can also pass in a parameter like, 2019-11-08T17:44:56.144.
moment("2019-11-08T17:44:56.144").format(moment.HTML5_FMT.DATE); // 2019-11-08
https://momentjs.com/docs/#/parsing/special-formats/
You can also use this format:
moment().format('ddd, ll'); // Wed, Jan 4, 2017
formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LL')
}
Look at these Examples.
Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // December 7th 2020, 9:58:18 am
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Dec 7th 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-12-07T09:58:18+05:30
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago
moment().startOf('day').fromNow(); // 10 hours ago
moment().endOf('day').fromNow(); // in 14 hours
moment().startOf('hour').fromNow(); // an hour ago
Calendar Time
moment().subtract(10, 'days').calendar(); // 11/27/2020
moment().subtract(6, 'days').calendar(); // Last Tuesday at 9:58 AM
moment().subtract(3, 'days').calendar(); // Last Friday at 9:58 AM
moment().subtract(1, 'days').calendar(); // Yesterday at 9:58 AM
moment().calendar(); // Today at 9:58 AM
moment().add(1, 'days').calendar(); // Tomorrow at 9:58 AM
moment().add(3, 'days').calendar(); // Thursday at 9:58 AM
moment().add(10, 'days').calendar(); // 12/17/2020
Multiple Locale Support
moment.locale(); // en
moment().format('LT'); // 9:58 AM
moment().format('LTS'); // 9:58:18 AM
moment().format('L'); // 12/07/2020
moment().format('l'); // 12/7/2020
moment().format('LL'); // December 7, 2020
moment().format('ll'); // Dec 7, 2020
moment().format('LLL'); // December 7, 2020 9:58 AM
moment().format('lll'); // Dec 7, 2020 9:58 AM
moment().format('LLLL'); // Monday, December 7, 2020 9:58 AM
moment().format('llll'); // Mon, Dec 7, 2020 9:58 AM
Whenever I use the moment.js library I specify the desired format this way:
moment(<your Date goes here>).format("DD-MMM-YYYY")
or
moment(<your Date goes here>).format("DD/MMM/YYYY")
... etc I hope you get the idea
Inside the format function, you put the desired format. The example above will get rid of all unwanted elements from the date such as minutes and seconds
With newer versions of moment.js you can also do this:
var dateTime = moment();
var dateValue = moment({
year: dateTime.year(),
month: dateTime.month(),
day: dateTime.date()
});
See: http://momentjs.com/docs/#/parsing/object/.
You can use this constructor
moment({h:0, m:0, s:0, ms:0})
http://momentjs.com/docs/#/parsing/object/
console.log( moment().format('YYYY-MM-DD HH:mm:ss') )
console.log( moment({h:0, m:0, s:0, ms:0}).format('YYYY-MM-DD HH:mm:ss') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
For people like me want the long date format (LLLL) but without the time of day, there's a GitHub issue for that: https://github.com/moment/moment/issues/2505. For now, there's a workaround:
var localeData = moment.localeData( moment.locale() ),
llll = localeData.longDateFormat( 'llll' ),
lll = localeData.longDateFormat( 'lll' ),
ll = localeData.longDateFormat( 'll' ),
longDateFormat = llll.replace( lll.replace( ll, '' ), '' );
var formattedDate = myMoment.format(longDateFormat);
Try this:
moment.format().split("T")[0]
The thing is - you can run into an issue with timezones. For example, if you parse date like this: '2022-02-26T00:36:21+01:00' it may turn into '25/02/2022' As a solution if your date is in ISO format you can just cut off the time portion from the string, like this:
moment('2022-02-26T00:36:21+01:00'.split('T')[0]).utc().format('DD/MM/YYYY')
This solution is quite blunt, so be careful with string format.
This format works pretty fine
const date = new Date();
const myFormat= 'YYYY-MM-DD';
const myDate = moment(date, 'YYYYMMDDTHHmmss').format(myFormat);
Try
new Date().toDateString()
Result - "Fri Jun 17 2022"
This worked perfectly for me:
moment().format('YYYY-MM-DD')
moment(date).format(DateFormat)
Here DateFormat should be DateFormat = 'YYYY-MM-DD'
Here is the date/time string I am passing into MomentJS
let value = '2018-07-21T17:09:51.000Z'
This should be 07/21/2018 # 5:09:51 PM
moment(value).format("dddd, MMMM Do YYYY, h:mm:ss a")
Moment returns: Saturday, July 21st 2018, 10:09:51 am
Why is it showing 10:09:51 am and not 5:09:51 PM ?
Thanks
Simple answer... as #jshamble indicated... make sure you are using the timzone method .utc()
Solution to my question in the example:
moment(value).utc().format("dddd, MMMM Do YYYY, h:mm:ss a")
I have a date which is displayed on 12 July 2013. I am using the format "dd MMMM yyyy". I want to display the month text i.e. July in Arabic text. Is there anyway?
Am getting يوليه as result instead يوليو, My client, saying: the month of July is “يوليو” in Arabic, while on the website it is showing as “يوليه”. Which is wrong,
can anyone help me on this?
Please find the code below.
What I have tried:
CultureInfo uiCulture1 = CultureInfo.CurrentUICulture; //'ar-AE'
DateTime dDateTime2 = DateTime.Parse(dt.ToString(), uiCulture1, System.Globalization.DateTimeStyles.AssumeLocal);
lblDate.Value = dt.ToString("dd MMMM yyyy");
try this
var dateTime = DateTime.Now;
var month = DateTime.Now.ToString("MMMM", new CultureInfo("ar-AE"));
Console.WriteLine(month);
result أكتوبر
I think the issue is the
dt.ToString()
in the Date.Parse. The dDateTime2 is getting set to 07 December instead of 12 July because I think the Culture in question is defaulting to dd-mm-yyyy for date format.
var dt = new DateTime(2013, 07, 12);
Console.WriteLine(dt.ToString("dd MMMM yyyy")); //12 July 2013
CultureInfo uiCulture1 = new CultureInfo("ar-AE"); //'ar-AE'
DateTime dDateTime2 = DateTime.Parse(dt.ToString(), uiCulture1, System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine(dDateTime2.ToString("dd MMMM yyyy")); // 07 December 2013
Console.WriteLine(dt.ToString("dd MMMM yyyy", uiCulture1)); //12 يوليو 2013
provide the culture info in dateTime.toString()
var dateTime = DateTime.Now;
var uiCulture = new CultureInfo("ar-AE");
Console.WriteLine(dateTime.ToString("dd MMM yyyy",uiCulture));
I have a calendar that posts the value in the following format
3 January, 2017
and when I convert it to carbon by doing
$carbon = Carbon::parse($data['due_date']);
echo $carbon;
I see 2016-01-03 20:17:00
My expected output is 2017-01-03
Try
$date = Carbon::createFromFormat('j F, Y', $data['due_date']);
Just use this 👇
echo date('Y-m-d', strtotime('3 January, 2017'));
I try do date_diff last month, but have a problem if month have 31 day he say I'm to 01 at this month. And if do 2 month later give good result. But again every month who have 31 days give result to 01.
To be more clear i example you:
{{ "now" | date("Y-m-d") }} {# 2015-07-31 #}
{{ "now" | date_modify("-1 month") | date("Y-m-d") }} {# 2015-07-01 #}
{{ "now" | date_modify("-2 month") | date("Y-m-d") }} {# 2015-05-31 #}
{{ "now" | date_modify("-3 month") | date("Y-m-d") }} {# 2015-05-01 #}
Any1 have any ideea why ? Because any if modify month with even number you go at month start not to the end of previous month.
Based on the answer here it seems that when PHP modifies a DateTime() object with the string '-1 month' it simply decrements the month value, if I understand correctly.
So given your example, you start with today's date: 2015-07-31.
PHP changes this to 2015-06-31. However, there are only 30 days in June. So it increments this up to the next date that makes sense, which is... 2015-07-01.
I tried replicating this with:
echo (new DateTime())->sub(new DateInterval('P1M'))->format('Y-m-d');
and:
$dt = new DateTime();
$dt->modify('-1 month');
echo $dt->format('Y-m-d');
and I got the exact same result in each case:
2015-07-01
So I guess it's just one of PHP's foibles. Pretty messy, a lot can happen in a day!