angularjs convert date format to IST - angularjs

I want to convert date from 2015-12-02T18:30:00.000+0000 to Wed Dec 02 2015 00:00:00 GMT+0530 (India Standard Time) using angularjs.

you can make use of angular's date filter . The formats are clearly given here
{{'2015-12-02T18:30:00.000+0000' | date:"EEE MMM dd yyyy HH:hh:ss 'GMT'Z '(India Standard Time)'"}}
The above code snippet will give you Thu Dec 03 2015 00:12:00 GMT+0530 (India Standard Time) in the output. that is the time when you calculate from +0000 to a +0530
Here is a working solution

Working fiddle
HTML:
<div ng:app ng:controller="Scoper">
Input: {{v}} <br />
Angular: {{v | date:'EEE MMM dd yyyy HH:mm:ss'}} GMT{{v | date:'+0530' }} (India Standard Time) <br />
</div>
JS
function Scoper($scope) {
var s = "2015-12-02T18:30:00.000+0000";
$scope.v = s;
}

Related

convert date with time into hour AM/PM format

Want to covert Thu Apr 28 2022 07:00:00 GMT+0530 into 7pm
UI is like this,
also want to covert that 7pm to Thu Apr 28 2022 07:00:00 format
Try this, if it is working for you
var time = new Date('Thu Apr 28 2022 19:00:00 GMT+0530');
console.log(
time.toLocaleString('en-US', { hour: 'numeric', hour12: true })
);
You can format your date like this:
<TimePicker
showMinute={false}
showSecond={false}
use12Hours
onChange={(val, data) => {
// Thu Apr 28 2022 07:00:00 format
console.log(val?.format('ddd, MMMM D YYYY, h:mm:ss'));
}}
/>
You can read more about formatting in moment documentation. Moment Format

how to take only times from dates inside array in rails

so... i need to take times from dates inside Array
so for example I have an array like this
a = [
Thu, 17 Mar 2022 10:00:00.000000000 KST +09:00,
Thu, 17 Mar 2022 10:00:00.000000000 KST +09:00,
Thu, 17 Mar 2022 14:00:00.000000000 KST +09:00,
Thu, 17 Mar 2022 14:00:00.000000000 KST +09:00,
Thu, 17 Mar 2022 17:00:00.000000000 KST +09:00]
and wanted to have result of
a = ["10:00", "10:00", "14:00", "14:00", "17:00"]
So i was trying this
can_choose =[];
a.each_with_index do |time| strftime("%H:%M")
can_choose <<|time|
but doesn't works at all...
where should I have to fix??
You can also extract the year, month, day the same way
https://ruby-doc.org/stdlib-3.1.1/libdoc/date/rdoc/Date.html
a = [
'Thu, 17 Mar 2022 10:00:00.000000000 KST +09:00',
'Thu, 17 Mar 2022 10:00:00.000000000 KST +09:00',
'Thu, 17 Mar 2022 14:00:00.000000000 KST +09:00',
'Thu, 17 Mar 2022 14:00:00.000000000 KST +09:00',
'Thu, 17 Mar 2022 17:00:00.000000000 KST +09:00']
can_choose =[]
a.map do |time|
t = DateTime.parse(time)
can_choose << t.strftime("%k:%M")
end
p can_choose
=> ["10:00", "10:00", "14:00", "14:00", "17:00"]
Ruby is all about message passing. The syntax to send a message is:
receiver.message(argument)
strftime is such message, but it needs a proper receiver. So if you have some Time instance, e.g.:
time = Time.parse('2022-03-17 10:00:00 +0900')
you'd write:
time.strftime('%H:%M') #=> "10:00"
with time being the receiver, strftime being the message and '%H:%M' being the argument.
You can use the above code in an each loop like this:
can_choose = []
a.each do |time|
can_choose << time.strftime('%H:%M')
end
Or you could use map to convert your array to another array:
can_choose = a.map { |time| time.strftime('%H:%M') }
The curly braces here { ... } are equivalent to the do ... end block above.
I would simple go with:
can_choose = a.map { |datetime| datetime.strftime('%H:%M') }
If you need only hours and minutes in 24h format, just use %R directive with Time#strftime or DateTime#strftime
can_choose = a.map { |time| time.strftime('%R') }

how to sort json array by date field

I have a JSON list of 2-D data as
var data = [
[
'Sun Feb 05 2019 00:00:00 GMT+0530 (India Standard Time)',
2,
5
],
[
'Sun Feb 06 2019 00:00:00 GMT+0530 (India Standard Time)',
5,
10
],
[
'Sun Feb 04 2019 00:00:00 GMT+0530 (India Standard Time)',
6,
2
]
];
Where the first item of each array items is a date.
I have to sort this list by date so that Feb 04 will come first while Feb 06 will come last.
How can I sort this list in JSON?
Create a Date object then compare the milliseconds: new Date(date).getTime()
const data = [
['Sun Feb 05 2019 00:00:00 GMT+0530 (India Standard Time)', 2, 5],
['Sun Feb 06 2019 00:00:00 GMT+0530 (India Standard Time)', 5, 10],
['Sun Feb 04 2019 00:00:00 GMT+0530 (India Standard Time)', 6, 2]
];
data.sort((a, b) => {
return new Date(a[0]).getTime() - new Date(b[0]).getTime();
});
console.log(data);

Change value of copy's scope change value of scope

I don't understand why to change value of copy, change the value of the $scope :
var tmpmember = $scope.registration.member;
console.log($scope.registration.member.birth);
tmpmember.birth=$filter('date')($scope.registration.member.birth,'yyyy-MM-dd');
console.log(tmpmember.birth);
console.log($scope.registration.member.birth);
Output :
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)
261 1954-03-11
262 1954-03-11
Someone could explain to me please ?
Many thanks
In the code you presented, you haven't made a copy of the object. Instead, you have created a second variable pointing to the same object instance.
Angular has a function you can use if you truly wish to have a copy rather than an additional reference, angular.copy. https://docs.angularjs.org/api/ng/function/angular.copy#!/
var tmpmember = angular.copy($scope.registration.member);
console.log($scope.registration.member.birth);
tmpmember.birth = $filter('date')($scope.registration.member.birth, 'yyyy-MM-dd');
console.log(tmpmember.birth);
console.log($scope.registration.member.birth);
Result:
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)
261 1954-03-11
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)

Find the most recent date from an Array

How to find the most recent date from an array like the one below?
Tue Jun 2 17:59:54 GMT+0200 2013
Tue Jun 5 18:00:10 GMT+0200 2013
Tue Jun 1 12:27:14 GMT+0200 2013
Tue Jun 3 17:26:58 GMT+0200 2013
Tue Jun 9 17:27:49 GMT+0200 2013
Tue Jun 1 13:27:39 GMT+0200 2015
Tue Jun 3 12:27:59 GMT+0200 2013
Tue Jun 6 15:27:22 GMT+0200 2014
Tue Jun 2 17:27:30 GMT+0200 2014
Assuming your array is full of AS3 native Date objects, you could simply do this:
array.sortOn("time",Array.DESCENDING);
trace("Most Recent:",array[0]);
You cannot use array.sort (unless you use the Array.NUMERIC flag) because it will sort the string representation of the date. So all your days of the week would then be grouped together instead of the actual date.
If your dates are strings, then you will need to convert them to Date objects prior to sorting:
//assuming your posted array is in a var called 'stringArray'
var dateArray:Array = []; //a new array to hold the converted strings
for(var i:int=0;i<stringArray.length;i++){
dateArray.push(new Date(stringArray[i]));
}
dateArray.sortOn("time",Array.DESCENDING);
trace("Most Recent Date:",dateArray[0]);
To show this in a concrete example, here is your posted dates - copy paste this code to produce the same results:
var arr:Array = new Array(
new Date("Tue Jun 2 17:59:54 GMT+0200 2013"),
new Date("Tue Jun 5 18:00:10 GMT+0200 2013"),
new Date("Tue Jun 1 12:27:14 GMT+0200 2013"),
new Date("Tue Jun 3 17:26:58 GMT+0200 2013"),
new Date("Tue Jun 9 17:27:49 GMT+0200 2013"),
new Date("Tue Jun 1 13:27:39 GMT+0200 2015"),
new Date("Tue Jun 3 12:27:59 GMT+0200 2013"),
new Date("Tue Jun 6 15:27:22 GMT+0200 2014"),
new Date("Tue Jun 2 17:27:30 GMT+0200 2014")
);
arr.sort(Array.DESCENDING);
trace("SORT:");
traceDates();
arr.sortOn("time",Array.DESCENDING);
trace("\nSORT ON:");
traceDates();
function traceDates(){
for(var i:int=0;i<arr.length;i++){
trace(" ",arr[i].fullYear + "-" + arr[i].month + "-" + arr[i].day);
}
}
//OUTPUT:
/*
SORT:
2013-5-3
2013-5-0
2013-5-0
2013-5-6
2013-5-1
2013-5-1
2014-5-1
2015-5-1 //most recent date, second to LAST item in the array
2014-5-5
SORT ON:
2015-5-1 //June 1st is the most recent date (first item in the array)
2014-5-5
2014-5-1
2013-5-0
2013-5-3
2013-5-1
2013-5-1
2013-5-0
2013-5-6
*/
Are they Date objects?
If so, you can compare the time property of each. It will give you the number of milliseconds since Jan 1, 1970. The highest number will be the most recent.
Something along these lines:
var mostRecentDate:Date = dateArray[0];
for(var i:int = 0; i < dateArray.length; i++){
if(dateArray[i].time > mostRecentDate.time){
mostRecentDate = dateArray[i];
}
}
Date objects act like simple Number when it comes to sorting or comparison. All you have to do is treat them like Numbers. So taken from Cadin answer:
dateArray.sort();
var oldestDate:Date = dateArray[0];
Will get you the oldest Date while:
dateArray.sort(Array.DESCENDING);
var mostRecentDate:Date = dateArray[0];
Will get you the most recent one.
For LDMS, this is what I got:
var firstdate:Date = new Date();
var seconddate:Date = new Date();
var thirddate:Date = new Date();
seconddate.time = firstdate.time + 5000000;
thirddate.time = firstdate.time + 50000000;
trace(seconddate > firstdate)//true
trace(firstdate > seconddate)//false
trace(seconddate.time > firstdate.time)//true
var array:Array = [thirddate, firstdate, seconddate];
trace(array)
//Wed Jun 3 03:37:40 GMT-0400 2015,Tue Jun 2 13:44:20 GMT-0400 2015,Tue Jun 2 15:07:40 GMT-0400 2015
array.sort();
trace(array)
//Tue Jun 2 13:44:20 GMT-0400 2015,Tue Jun 2 15:07:40 GMT-0400 2015,Wed Jun 3 03:37:40 GMT-0400 2015
array.sort(Array.DESCENDING);
trace(array)
//Wed Jun 3 03:37:40 GMT-0400 2015,Tue Jun 2 15:07:40 GMT-0400 2015,Tue Jun 2 13:44:20 GMT-0400 2015
Sort the array and grab the first (descending) or last (ascending) element.
Edit: 2 down-votes because I didn't provide an example, or because people don't know you can sort dates? This works:
var dates:Array = [
"Tue Jun 2 17:59:54 GMT+0200 2013",
"Tue Jun 5 18:00:10 GMT+0200 2013",
"Tue Jun 1 12:27:14 GMT+0200 2013",
"Tue Jun 3 17:26:58 GMT+0200 2013",
"Tue Jun 9 17:27:49 GMT+0200 2013",
"Tue Jun 1 13:27:39 GMT+0200 2015",
"Tue Jun 3 12:27:59 GMT+0200 2013",
"Tue Jun 6 15:27:22 GMT+0200 2014",
"Tue Jun 2 17:27:30 GMT+0200 2014"
].map(function(s:String, i:int, a:Array):Date {
return new Date(s);
}).sort(Array.NUMERIC | Array.DESCENDING);
var latest:Date = dates[0]; // Mon Jun 1 07:27:39 GMT-0400 2015
The problem is the OP did not make it clear what kind of data they are working with (strings or Date objects) so the exact solution code is unknown.

Resources