Get day from a Date - React Native - javascript

in my program when I click on a calendar I am getting the "selectedDate" variable changed. Like if I click on 4/6/2021 the value of selectedDate = 04-06-2021, Like if I click on 14/7/2021 the value of selectedDate = 14-07-2021.
I need to get which day of the week the dates are. I am trying in this way:
<View>
<H1 color={color.white}>{days[selectedDate.getDay()]}</H1>
</View>
I need to get which day of the week is the selected date. this syntax is giving an error "Property 'getDay' does not exist on type 'string'"

Use moment https://momentjs.com/
import moment inside your file
moment().format('dddd');
this is the method to get day from a given date

you can use moment(https://momentjs.com/) for this. With the help of moment we can get date in different formats. I hope you this will help

Related

Searching in input gives incorrect date in material date picker need to convert it into UTC

I am extending MomentDateAdapter for my requirement. When i am selecting a date from the calendar i am getting the correct output but when i manually type something in the input field i get wrong output.
For the selected date i am using _moment.utc({ year, month, date }).locale(navigator.language); to convert the selected value to UTC format but i am not sure on how to do the same when user searches in the input field.
StackBlitz.
to reproduce:
Try to select a value from calendar and see the console (notice the date is converted to UTC)
Now try to add a date manually by typing in and see the console (date is not converted to UTC).
You need to adapt your parse method call of moment to:
return moment.utc(value, parseFormat, this.locale, true);
to get utc Date from your input.
Here is your adapted Stackblitz.
The methods format and createDate are called if you set your date via picker, the parse method is called if you set it via input.

How to get last day of a given year with moment?

I can hard-code the last day of a year because anyway it will always be 31st December. There are many solutions using date, js, jquery. But I'm working on an Angular project and hence my code is in typescript. My tech lead wants me to do this using moment. I'm not supposed to hard-code any date. Is there any built-in method provided by moment to fetch the last day of a given year. And I'm saying given year because in my case given year is dynamic. It can change anytime. I'm using endOf() method. I'm getting error with custom year. I mean:
const lastDayOfYear = moment().endOf('year') is working fine, but:
const lastDayOfYear = moment().endOf(2025) is giving me this error:
Argument of type '2025' is not assignable to parameter of type 'StartOf'
I also tried:
const lastDayOfYear = moment().endOf("2025");
year=2025; const lastDayOfYear = moment().endOf(year);
How will this method work? I've gone through the entire Moment docs. Or should I stick to Date of javascript. Something like this:
new Date(new Date().getFullYear(), 11, 31);
Please provide a solution.
PS: I want last date of a given year in MM-DD-YYYY format only.
Well if you really wanted to work that out with moment instead of using `12-31-${year}` you could instead use:
moment([year]).endOf('year').format('MM-DD-YYYY')
moment(date).endOf('year');
Where date is a Date somewhere in that year.
set current date in moment new Date()
const now = moment(new Date()).endOf('year');
alert(now);

In JavaScript/AngularJS, I can't seem to remove the "T" in a date field from the DB?

I must be missing something very obvious, but I could use help. I have an Angular web app, which is getting data from a MS Web API. The DTO object has two date fields. When those fields come in, there is a "T" in the date field.
e.g. 2017-05-01T03:43:55
For whatever reason, I can't seem to get rid of that darn "T" when using the date. I'd tried using a date format both on the calendar control that displays the date data, and using the JavaScript format() command. I also tried using the Javascript replace() command to replace the "T" with a space. And I tried using Moment.js to create a new date (which seems to fail and give me an invalid date).
var startDate = moment(badge.startDate).format('yyyy-MM-dd HH:mm:ss');
var startDate = badge.startDate.format(''yyyy-MM-dd HH:mm:ss'');
var startDate = moment(badge.startDate).replace('T',' ');
What am I missing? How should I be doing this? As far as I can tell, any of these should work?
You can create separate filter for that as below.
return (input) ? $filter('date')("2017-05-01T03:43:55", "yyyy-MM-dd HH:mm:ss") : '';
You can change for of date according to your requirements. You can see details Below.
https://docs.angularjs.org/api/ng/filter/date
use, don't build what already exists!
First visit moment js this component is very helpful and has many options which you can do with a date.
var date = moment(yourDate).format('YYYY-MM-DD');
format has a lot of options, you can find it on moment js documents.

Decrease bootstrap datepicker end date

I am using bootstrap datepicker for one on my project and set endDate like this
$('#daterange').data('daterangepicker').setEndDate('2017/03/12');
EndDate is somthign that i get from database, only problem is that on datepicker i have to set endDate but decrease one date, it measn if i have something from server like this
2017/03/12
I need to bind datepicker like this
$('#daterange').data('daterangepicker').setEndDate('2017/03/11');
is it possible to do that in frontend or i must go to server to correct, any simple solution?
you can use momentjs
moment("your date", "YYYY/MM/DD").subtract(1, 'days').format('YYYY/MM/DD');
What i will do:
1. get date from database ( 2017/03712)
2. change it to datetime format
3. decrease one day
4. setEndDate with correct format

How to use moment js for displaying formatted date?

Guys I am getting date through ajax call in my ASP.net mvc application. Now the date I am getting is in the format of 2014-06-09T17:38:55 however I want to display it in three formats:
Like this 06/09/2014 05:38:55 PM
Like three hours ago or Yesterday style (used by facebook)
Just the date part like 06/09/2014
Basically I have a hidden input field in my view which gets the Date from ajax call. Now I am doing something like this :
<input id="hiddenLastAlertDate" type="hidden" value="values.CreationDate" />
In my script section, I have a function in which I am doing something like this:
var datefield = document.getElementById("hiddenLastAlertDate").value;
formattedDate = moment(datefield).format('L'); //for getting the date part only
Upon running the application,formattedDate shows up as undefined. Why is that? I think I was unable to follow the moment.js documentation. How can I use moment.js to get this formatted result ?
moment("20140618 08:00", "YYYYMMDD hh:mm").fromNow(); will return 2 hours ago
moment("20140617", "YYYYMMDD").fromNow(); will return a day ago
moment().format('DD/MM/YYYY h:mm:ss A'); will return 18/06/2014 9:42:24 AM

Categories