How to convert string to date in react native with moment.js? - reactjs

I have this date in string, now i want to convert this with UTC using moment.js.
Wednesday, 22 September 2021, 7:00:00 am
Expected result:
Formate: DD-MMM-YYYY hh:mm a
Date: 22-Sep-2021 12:30 PM
I tried this way but not working
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
utcDate = moment(moment(dateStr, 'DD-MMM-YYYY hh:mm a'))
.utc()
.format('DD MMM YYYY, hh:mm A');
Also I tried this conversion with dayjs but it works well in iOS but not in Android where I get null.
utcDate = dayjs(new Date(`${dateStr} UTC`)).format(
'DD MMM YYYY, hh:mm A',
);
let me know if anyone have solution for this.
Thanks!

Try this:
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
const utcDate = moment(new Date(dateStr))
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(utcDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

You need to remove the format from moment call.
You can do this but it is a deprecated way
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
// You can do this but it is deprecated way
const date = moment(dateStr)
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
The better solution.
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
const date = moment(new Date(dateStr))
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related

Array find per day React

I have an array of schedule that I only want to show 1 data in the array depending on the day today.
{
schedule.map(s => {
let days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thurdsay', 'friday', 'saturday'];
let date = new Date();
let dayIndex = date.getDay();
let dayName = days[dayIndex]
let currentSchedule = schedule.find(s => s.days == dayName)
if(currentSchedule){
return <li className="list-group-item">{currentSchedule.day}: Open: {s.open} - Close: {s.close}</li>
}
})
}
current output is
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
My expected output is
for example today is saturday it will only output
Satruday: opening: 10:00 AM PST - closing: 10:00 PM PST
Map() function iterate through all elements of array. You should use filter() function instead of it. Try this code, it should work correctly.
schedule.filter(s => {
let days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thurdsay', 'friday', 'saturday'];
let date = new Date();
let dayIndex = date.getDay();
let dayName = days[dayIndex];
let currentSchedule = schedule.find(s => s.days == dayName);
if(currentSchedule){
return <li className="list-group-item">{currentSchedule.day}: Open: {s.open} - Close: {s.close}</li>
}
})
But it`s also better to take out duplicate code into the definition of scheduler.
let days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thurdsay', 'friday', 'saturday'];
let date = new Date();
let dayIndex = date.getDay();
let dayName = days[dayIndex];

Getting only "today and yseterday" from ngx-moment in angular 9

I'm using ngx-moment pipe to display my date
it's really helpful and very useful
But I want to display the date from today and tomorrow and then the calendar date like that
if the date today then "Today and 00:00pm", it tomorrow then "Yesterday at 00:00pm",
Then if the day after tomorrow I need to show the calendar date "13/9/2020"
here is the code
date:'2020-09-13T00:00:00' ;
<div>Last updated: {{date| amTimeAgo}}</div>
I solved it
moment.updateLocale('en', {
calendar : {
lastDay : '[Yesterday] LT',
sameDay : '[Today] LT',
nextDay : 'LL',
lastWeek : 'LL',
nextWeek : 'LL',
sameElse : 'LL'
}
});

MomentJS invalid date

I'm trying to convert a date string like 'DD-MM-YYYY', to 'YYYY-MM-DD'.
Unfortunately I'm getting a invalid date.
var startDate = $('.startDate').val();
var endDate = $('.endDate').val();
var newstartDate = moment(startDate).format( "YYYY-MM-DD");
endDate = moment(endDate, "YYYY-MM-DD");
console.log('startDate:',startDate)
console.log('newstartDate:',newstartDate)
console.log('endDate:',endDate)
How could I get the output as: 2018-09-14 . / 2018-08-14 ?
var startDate = "22-12-2009";
var endDate = "01-02-2009";
var newstartDate = moment(startDate,'DD-MM-YYYY').format( "YYYY-MM-DD");
endDate = moment(endDate, 'DD-MM-YYYY');
console.log('startDate:',startDate)
console.log('newstartDate:',newstartDate)
console.log('endDate:',endDate)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Specify the input date format when parsing with moment moment(startDate,'DD-MM-YYYY').format( "YYYY-MM-DD");

Convert datetime format in string to date javascript?

Let say I have an object product
I can use product.createdDate to get 2018-04-16 15:12:46.179427 or any other date with the same format.
how do I convert to date in javascript so I and show it as 16 April 2018?
I'm using angularjs.
I tried {{product.createdDate | date : 'dd MM, yyyy'}}but it show 2018-04-16 15:12:46.179427
There is no straightforward way to do it. You can find your answer in a similar question here :
Get current date in DD-Mon-YYY format in JavaScript/Jquery
You can try the following:
var date = new Date("2018-04-16 15:12:46.179427".replace(/-/g,"/"));
var d = moment(date).format('D MMMM YYYY')
console.log(d);
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
You can use date pipe to filter it out
html
<p>
{{date | date:'dd MMMM yyyy'}}
</p>
In your controller
$scope.date = new Date("2018-04-16 15:12:46.179427");
To get directly in controller inject $filter
var date = $filter('date')(new Date("2018-04-16 15:12:46.179427"), 'dd MMMM yyyy');
console.log(date); // 16 April 2018
Please refer the demo link
To convert this date using javascript you have to define the months like bellow.
const months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const date = new Date('2018-04-16 15:12:46.179427');
const formatedDate = date.getDate() + ' ' + months[date.getMonth()] +' '+ date.getFullYear();
But if you only want to show it in the UI, use a pipe to convert it.

Find difference between 2 dates with AngularJS

I get 2 dates from MySQL something like this:
Start: 2015-11-01 22:56:59 End: 2015-11-03 00:00:00
Also I get dates from object array; here is what one object looks like:
Array[5]
0: Object
$$hashKey: "object:9"
created_at: "2015-10-23 03:36:11"
expiration_date: "2015-11-03 00:00:00"
id: 1
name: "TEST PROJECT"
I need to find how many days left with AngularJS...
Its simple. Use the function as:
$scope.calcDiff = function(firstDate, secondDate){
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
}
Consider using moment.js for all your javascript date and time manipulations.
With the moment library it is as easy as:
moment(date1).diff(moment(date2), 'days')
You just need to write a js function, that do all calculation instead of you.
<label>{{diffDate(date1, date2) }} </label>
$scope.diffDate = function(date1, date2){
var dateOut1 = new Date(date1); // it will work if date1 is in ISO format
var dateOut2 = new Date(date2);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
return dateOut;
};
If your data is not in ISO format, you have to set it explicitly.
var dateString = "2015-01-16 22:15:00";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
You can use amDifference filter. Don't forget to inject $filter
let dif = $filter('amDifference')(date1, date2, 'days');

Resources