How to get "Yesterday 17th May 2015" format in moment.js - momentjs

I need to format some days in the format "Yesterday 17th May 2015" using moment.js. This is what I have so far:
var asd = moment(moment.utc("2015-09-30T08:35:40.59"))
.subtract(1, 'days')
.calendar();

As described in the Calendar customization doc is it possibile to customize the format of calendar.
In the example below, I customized the sameElse format of calendar for the English locale to return a string with the format you required:
moment.locale('en', {
calendar : {
sameElse : '[Yesterday] Do MMM YYYY'
}
});
var asd = moment.utc("2015-09-30T08:35:40.59")
.subtract(1, 'days')
.calendar();
console.log(asd);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>

Related

typescript - Is there a way to output the long date from datapicker

In typescript, in this date-picker, after selecting a date from the calendar, the selected date is outputted in the format dd-mm-yy, is there a way to output the date in the long format(Wednesday 9th March 2022.) This is using the standard datepicker.
<input type="date" onChange={
(event:React.ChangeEvent<HTMLInputElement>) =>{
let newTimesheet = [...timesheet];
newTimesheet[index].date=event.target.value;
setTimesheet(newTimesheet);
}
}/>
Try this:
setTimesheet(new Intl.DateTimeFormat('en-GB', { dateStyle: 'full' }).format(newTimesheet));
i hope it works!
You can do this easily via Intl although the formatting is not the same as yours:
new Intl.DateTimeFormat("en-US", { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }).format(date); // Wednesday, March 9, 2022
Intl Documentation

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',
});

Convert datepicker date to ISO format with moment.js

My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.

Resources