get solar year in react - reactjs

how can I get current solar year local fa (not full date) in react?
I just need to get solar year (jalali) and I'm using this:
new Date().toLocaleDateString('fa-IR');
but it return current date (24/04/1399). I just want (1399)

You can pass additional options, and transform output as you want
new Date().toLocaleDateString('fa-IR', {year: 'numeric'} );

I find that.
the answer is :
new Date().toLocaleDateString("fa-IR", {year: "numeric" });
the above code return 1399. (current jalali year (persian date))

new Date().getFullYear(); // 2020

Related

Date picker: Cannot scroll throug all years using ngbDatepicker

Expected behavior
I want a user to be able to select all years in the year range in the date picker in hijri.
Actual behavior
A user can only choose 15 in the range. Say the range goes from 1436 - 1417, the user would first have to click on 1417, and then he would click the list again and could now select down to 1416. How to I modify the code so a user can scroll from all years and don't have to click multiple times to get to the right year?
All you have to do is to set minDate and maxDate like that:
<input class="form-control" (click)="d.toggle()" placeholder="yyyy-mm-dd"
[maxDate]="maxDate" [minDate]="minDate" [startDate]="startDate"
placement="bottom"
name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker">
and in typescript code :
maxDate ={year: new Date().getUTCFullYear()-16,month: 12, day: 31}
minDate ={year: new Date().getUTCFullYear()-100,month: 12, day: 31}
startDate={year: new Date().getUTCFullYear()-20,month: new Date().getUTCMonth(), day: 1}

ReactJS Datepicker - I want the datepicker to start from this month and on only

How can I make that ReactJS Datepicker should only display days starting from this month and on (no back dates).
This is not exactly what I was trying to do but its a quick fix.
I use the includeDates={this.state.includeDates} from ReactJS Datepicker
and I made a custom function to print out the days starting from today and i save it in an array that i then pass to the react state excludeDates: [''],
and only those days will be clickable.
let fromDate = moment();
let toDate = moment().add(24, 'months'); //including only days starting from today untill 2 year
for (let i = 0; i < moment(toDate).diff(fromDate, 'days') + 1; i++) {
state.includeDates.push(moment(fromDate).add(i, 'days'));
}
Thanl you! hope it helps someone.
You can use moment startOf using 'month' parameter to get the first day of the month and pass it to minDate option to make the datepicker enable only dates from the start of the current month:
<DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
minDate={moment().startOf('month')}
/>
If you want to enable only future dates, simply remove maxDate option from the linked example, you can use the following code:
<DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
minDate={moment().startOf('day')}
/>

converting date format in angularjs controller

i write the following coding to print the current date time
$scope.date = new Date();
and then i print the same using consol.log
console.log($scope.date);
and it is working fine
Tue Jan 24 2017 16:36:06 GMT+0530 (India Standard Time)
but now i want to change the date format and i want to print like
21-12-2016
can anybody help me here?
i used the conversion but i am unable to remember the page or the url of the page right now,
and stuck on this,
before i leave for the home today i thought of solving this issue
In controller you can do
$filter('date')(date, format, timezone)
to change the date format. And in html,
{{ date_expression | date : format : timezone}}
use this.
Like
$scope.formattedDate = $filter('date')($scope.currDate, "dd-MM-yyyy");
to print same on html
{{ currDate | date : "dd-MM-yyyy"}}
https://docs.angularjs.org/api/ng/filter/date
Following formats are supported by angular.
You can do this either in controller or in html page.
$scope.date = new Date();
The first one is :
$scope.date = $filter('date')($scope.date, 'dd-MM-yyyy');
Second one is :
{{date | date:'dd-MM-yyyy'}}
You can use the Angular date filter:
{{date | date: 'dd-MM-yyyy'}}
You can use the in-build js libraries functions i.e getDay(), getHours(), getMinutes(), getMilliseconds(). This functions will return you the corresponding date's individual components values.
e.g
var x = $scope.yourDateModelObj.getHours();
Likewise, you can get the date, month, years values.
return an integer value for hours.
Hope that helps

How to get local date with Timezone(EDT/PST) in Angular JS using moment?

I want the current local(to browser) date with Time zone in the following format:
26-Apr-2016 15:56:18 EDT
I have tried using moment.js but it shows date with timezone like GMT -04:00. Also tried this, but it didn't help.
var dateDDMMMYYYYFormate = $filter('date')(new Date(input), 'dd-MMM-yyyy HH:mm:ss');
var dateWithUtc = moment.utc(dateDDMMMYYYYFormate);
var localDate = moment(dateWithUtc).local();
Any help is appreciated.
Since new Date() creates a JavaScript date object, you want to change the moment object into a date object:
var localDate = moment().toDate();

How to convert timestamp to readable calendar date

I have timestamp date format from hosting database, My problem now is how to convert this timestamp format to readable calendar date and put inside the calendar, the process of converting will be done inside the calendar javascript I think this is possible my problem is how can I do that? I can now translate the to calendar format by using this if I have given declaration of timestamp format.
var timestamp = 1441987200;
var date = new Date(timestamp * 1000);
datevalues = [
date.getFullYear(),
date.getMonth()+1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
];
alert(datevalues);
But I cant put it inside my calendar because of different format of my plugin calendar with this new Date(y, 9, 2).
$scope.events =[ {title: 'Long Event',start: new Date(y, 9, 2),end: new Date(y, 10, 7)}],
Can anyone help me to find the solution for this? Thanks and regards.
I recommend using MomentJS when you have to handle a lot of times and dates. It's able to parse times in a more convenient way than JavaScript's Date and it's also able to format it just the way you like.

Resources