Need some help. Try compare string time with realt time.
<div ng-if="{{currentTime | date:'H:M'}} > {{stingtime}}">Hide</div>
Where currentTime is:
$scope.currentTime = new Date();
And {{stingtime}} is parsed from JSON time looks like 18:35
Finnaly I try compare time ex: 13:45 with string 18:45
Related
Is there any way to convert normal string 5pm into 2022-04-20T17:00:00.000Z format?
I have got this from backend but Im using timepicker in antd. It only accepts 2022-04-20T17:00:00.000Z format and it is in string format in my DB.
This should consistently give you the current date with the time attached
function convertToISO(timeString) {
const [hour12, ampm] = timeString.split(/(?=[ap]m$)/i)
const hour = hour12 % 12 + (ampm.toLowerCase() === 'pm' ? 12 : 0)
const date = new Date()
// Set time, adjusted for time zone
date.setHours(hour, -date.getTimezoneOffset(), 0, 0)
return date.toISOString()
}
console.log(convertToISO('5pm'))
I'm facing some difficulties to get the right datetime using the momentjs library.
I'm doing this
****--------- Datetime ----------****
Timestamp received from database -> 2016-07-12 17:21:40 <- THIS IS A UTC DATE TIME
moment.tz(datetime, moment.tz.guess()); //datetime value is '2016-07-12 17:21:40' and moment.tz.guess() is returning "America/Sao_Paulo"
// inspect the moment object returned
q{ _isAMomentObject:true, _i:"2016-07-12 17:21:40", _f:"YYYY-MM-DD HH:mm:ss", _isUTC:true, _pf:Object…}
_a: Array[7]
_d:Tue Jul 12 2016 14:21:40GMT-0300 (BRT) <- THIS IS THE CORRECT DATE TIME THAT I WANT
_f:"YYYY-MM-DD HH:mm:ss"
_i:"2016-07-12 17:21:40"
_isAMomentObject:true
_isUTC:true
_isValid:true
_locale:B
_offset:-180
_pf:Object
_z:h
__proto__:Object
// moment object formatted .format('YYYY-MM-DD HH:mm:ss')
2016-07-12 17:21:40 <- THIS IS THE DATE TIME THAT I'M GETTING, AND IT IS WRONG
****-------------------****
I don't know what I'm doing wrong here.
Solution:
I found out how to archive this.
moment.tz(moment.utc(datetime), moment.tz.guess());
I receive a number representing a date in ddmmyy format eg.: 250615 i need transform into this format : 25/06/15.
I canot use the angular date filter because i receive a number.
I try to use slice in this way but only print the first number
{{ singularitem.Dia.slice(0,2)}} {{ singularitem.Dia.slice(2,2) }} {{ singularitem.Dia.slice(-2,2) }}
How can it be done ?
Thank´s in advance
Since you can't use the date filter, just create your own filter, doing whatever you want it to do:
app.filter('transformDate', function() {
return function(date) {
// Split string at every second character and combine them again with a `/` in between
return date.match(/.{1,2}/g).join('/');
}
})
Usage:
{{date | transformDate}}
Plunker: http://plnkr.co/edit/uBeT0eHOoAlHDUWydOpM?p=preview
You can go for a vanilla javascript function. If your format is always dd/mm/yy check this function:
(function () {
var date = "250615";
date = date.slice(0, 2) + "/" + date.slice(2,4) + "/" + date.slice(4,6);
console.log(date);
})()
FIDDLE
Slice is a prototype function of Strings and Arrays, not numbers. You need to convert your number to string first.
If you want to convert that number to a date object, you'd need:
function parseDate(dateAsNumber) {
var dateAsString = "" + dateAsNumber;
var day = Number(dateAsString.substr(0, 2));
//months are index 0
var month = Number(dateAsString.substr(2,2)) - 1;
//assumming all dates are 2000+ since you only get 2 digits
var year = Number(dateAsString.substr(4,2)) + 2000;
var date = new Date(year, month, day);
return date;
}
Calling parseDate(250615) returns Thu Jun 25 2015 00:00:00 GMT-0700 (Pacific Daylight Time)
I've tried the angular docs of {{someDate | date:'params'}}, however; This doesn't work if someDate is in this format yyyy-MM-dd HH:mm:ss.
How would I convert yyyy-MM-dd HH:mm:ss to say just yyyy using angularjs.
The date filter is designed to handle multiple input types including date objects and common date strings. If your date string is not recognized by the provided date filter, I would just write my own filter. It would look something like this...
myapp.filter('toYear', function () {
return function (dateString) {
var dateObject = new Date(dateString);
return dateObject.getFullYear();
};
});
You can use it like this...
{{someDate | toYear}}
You can define a custom format as follows
Date.prototype.customFormat = function(){
return this.getMonth() +
"/" + this.getDate() +
"/" + this.getFullYear();
}
//usage
> var d = new Date()
Fri Mar 14 2014 15:39:07 GMT+0000 (GMT Standard Time)
> d.customFormat()
'2/14/2014'
More details https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
I actually fixed the solution here:
app.filter('timestampToISO', function() {
return function(input) {
input = new Date(input).toISOString();
return input;
};
});
http://bit.ly/1iJAV8G
UPDATE:
To implement you add this to the view:
{{someSQLTIMESTAMP | timestampToISO | date}}
Angular js date object doesn't support to convert from MySql date format to timestamp directly. So need to convert it to acceptable format from YYYY-mm-dd H:i:s to YYYY/mm/dd H:i:s and then convert it to a timestamp.
You may try the below snippet to convert Mysql date format YYYY-mm-dd H:i:s to timestamp using AngularJs filter
app.filter("mysqlDateFormatToTimestamp", function(){
return function(date){
var date1 = date2 = date3 = timestamp = hours = minutes = seconds = '';
date1 = date.split(':');
date2 = date1[0].split(' ');
date3 = date2[0].split('-'); // Change it based on your format
if( date1.length == 1 && date2.length == 1 ){
hours = '00';
minutes = '00';
seconds = '00';
}else{
hours = parseInt(date2[1]);
minutes = parseInt(date1[1]);
seconds = parseInt(date1[2]);
}
timestamp = new Date(parseInt(date3[0]), parseInt(date3[1])-1, parseInt(date3[2]), hours, minutes, seconds);
return timestamp;
}
});
Now, You may use this filter wherever the date format you want, and you may change the params as you like.
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'LLLL dd, yyyy HH:mm:ss'}}
// results would comes like "July 07, 2016 05:27:30"
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'yyyy'}}
// results would comes like "2016"
my case is I have a Date obj the date inside is UTC time. However I want it to be changed to Japan time.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
calendar.setTime(someExistingDateObj);
System.out.println(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + calendar.get(Calendar.MINUTE));
the existingDateObj is mapped from db and db value is 2013-02-14 03:37:00.733
04:37
it seems the timezone is not working?
thanks for your time....
Your problem may be that you're looking at things wrong. A Date doesn't have a time zone. It represents a discrete moment in time and is "intended to reflect coordinated universal time". Calendars and date formatters are what get time zone information. Your second example with the Calendar and TimeZone instances appears to work fine. Right now, this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
System.out.println(String.valueOf(calendar.get(Calendar.HOUR)) + ":" + calendar.get(Calendar.MINUTE));
}
Reports:
0:32
That appears correct to me. What do you find wrong with it?
Update: Oh, perhaps you're expecting 12:32 from the above code? You'd want to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR for that, or else do some hour math. Calendar.HOUR uses 0 to represent both noon and midnight.
Update 2: Here's my final attempt to try to get this across. Try this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("H:mm a Z");
List<TimeZone> zones = Arrays.asList(
TimeZone.getTimeZone("CST"),
TimeZone.getTimeZone("UTC"),
TimeZone.getTimeZone("Asia/Shanghai"),
TimeZone.getTimeZone("Japan"));
for (TimeZone zone : zones) {
calendar.setTimeZone(zone);
format.setTimeZone(zone);
System.out.println(
calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + " "
+ (calendar.get(Calendar.AM_PM) == 0 ? "AM " : "PM ")
+ (calendar.get(Calendar.ZONE_OFFSET) / 1000 / 60 / 60));
System.out.println(format.format(calendar.getTime()));
}
}
Note that it creates a single Calendar object, representing "right now". Then it prints out the time represented by that calendar in four different time zones, using both the Calendar.get() method and a SimpleDateFormat to show that you get the same result both ways. The output of that right now is:
22:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
12:59 PM 8
12:59 PM +0800
13:59 PM 9
13:59 PM +0900
If you used Calendar.HOUR instead of Calendar.HOUR_OF_DAY, then you'd see this instead:
10:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
0:59 PM 8
12:59 PM +0800
1:59 PM 9
13:59 PM +0900
It correctly shows the current times in Central Standard Time (my time zone), UTC, Shanghai time, and Japan time, respectively, along with their time zone offsets. You can see that they all line up and have the correct offsets.
sdf2 and sdf3 are equaly initialized, so there is no need for two of them.