Converting mysql date to js date works in chrome but not safari - mysql

I am using a pipe in angular2 to convert a mysql date to a js date format here is my code:
export class DateToIso {
transform(value) {
let date = new Date(value);
let str = (date.getMonth() + 1) + '.' + date.getDate() + '.' + date.getFullYear()
return str;
}
}
In HTML I use
{{ post[2] | dateToIso}}
to show the converted date. In Chrome I get the correct datetime but not in safari. It returns NaN.NaN.NaN.

I had a similar problem with the currency pipe and it was an internationalization issue. I wrote it up here http://blogs.msmvps.com/deborahk/angular-2-getting-started-problem-solver/
Install the international package: npm install intl#1.1.0 –save
Include the following in index.html:
(I'm using a touch device and the pasted code doesn't work. Please see the link)

I tried it #DeborahK but it didn`t work so I just took the Mysql date "String" and made substrings out of it and rearranged the substrings to a new string and returned it.
export class DateToIso {
transform(value, args) {
let month = value.substr(5,2);
let day = value.substr(8,2);
let timeThen = value.substr(11,5);
let newValue = day + "." + month + " um " + timeThen;
return newValue;
}
}
I believe this is not the best solution but it is working. Hope to get better solutions soon.

Related

Comparing 2 strings from HTML file - Angular

I am using angular 7 and I would like to know how to compare two strings. In this case, each of my strings simulates one date, let's say "2019-12-26" and "2018-12-26".
In Javascript is pretty simple to compare them since I just need to use the operators:
console.log(today > "2018-12-06");
It is working how I supposed it was gonna work. It basically returns true. Nevertheless, I am trying to do exactly the same from my HTML file
<div *ngIf="today > example.endDate">
being today and 'example.endDate' two strings containing exactly the same strings that I used for the Javascript example, but it does not show any of them.
Is there any other way to make this comparison?
Regards,
Mario
UPDATE
I have had a second look at the problem and it seems that the comparison is not a problem, but the way of getting the variable is.
I get a variable in ngOnInit().
ngOnInit() {
this.getCurrentDate();
}
//Get current date
getCurrentDate() {
let aux = new Date();
//2 characteres
let dd = String(aux.getDate()).padStart(2, "0");
let mm = String(aux.getMonth() + 1).padStart(2, "0"); //January is 0!
let yyyy = aux.getFullYear();
let today = yyyy + "-" + mm + "-" + dd;
let other = "2019-01-31";
}
The problem is that I use this variable directly in my HTML how I previously showed. The error I get is the following:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression
has changed after it was checked. Previous value: 'ngIf: undefined'.
Current value: 'ngIf: true'.
So the problem is that I am using a variable in the HTML file before getting the value. Or at least it is what I understand
To check this error I have created a Stackblitz representation. On it, I have created two examples:
Variables not on ngOnInit()
Variables in ngOnInit()
https://stackblitz.com/edit/angular-jjgsmq?file=src%2Fapp%2Fhello.component.ts
the most simple solution is just to
and are you sure that ngOnInit is the right LifeCycle hook for you?
I would try ngAfterContentInit() if the component is "heavy" to render other wise ngAfterViewInit() would have been my choice
<div *ngIf="IsTodayBigger()">
ngOnInit() {
this.getCurrentDate();
}
IsTodayBigger(): boolean {
today=this.getCurrentDate()
exampleEndDate= example.endDate;//use binding or ViewChild if needed
return today&&exampleEndDate&& today> example.endDate
}
//Get current date
getCurrentDate() {
let aux = new Date();
//2 characteres
let dd = String(aux.getDate()).padStart(2, "0");
let mm = String(aux.getMonth() + 1).padStart(2, "0"); //January is 0!
let yyyy = aux.getFullYear();
today = yyyy + "-" + mm + "-" + dd;
let other = "2019-01-31";
}

How to program a URL? (For search query)

A co-worker of mine shared an autohotkey script (it's actually an exe file that runs on the background). Anyways, when I click the hotkeys it opens up a company webiste and creates a shared query for whatever's on the clipboard. I was wondering how this is done and how I can make my own.
I'm specially curious about the "URL" modification that includes all these search options:
https://<COMPANYWEBSITE>/GotoDocumentSearch.do
That's the URL where I can search (sorry it's restricted and even if I link it you cant access it).
Anyways, after I set up all my options and stuff and click the search button I get the following URL:
https://<COMPANYWEBSITE>/DocumentSearch.do
I inspected the website source and this is the function that's called when I press the search button:
function preSubmitSearch(docPress) {
document.pressed = docPress;
// setup local doc types for submit by lopping over multi selects and building json data string
var localDocTypesJson = "{";
var sep = "";
jQuery(".localTypeSel").each(function (i) {
var selLocalTypes = jQuery(this).multiselect("getChecked");
// get doc type code from id ex. 'localTypeSel_PD'
//window.console.log("this.id=" + this.id);
var tmpArr = this.id.split("_");
var docTypeCode = tmpArr[1];
var selLocalTypesCnt = selLocalTypes.length;
if (selLocalTypesCnt > 0) {
var localTypes = "";
var sep2 = "";
for (var i2 = 0; i2 < selLocalTypesCnt; i2++) {
localTypes += sep2 + "\"" + selLocalTypes[i2].value + "\"";
sep2 = ",";
}
localDocTypesJson += sep + "\"" + docTypeCode + "\": [" + localTypes + "]";
sep = ",";
}
});
localDocTypesJson += "}";
jQuery("#localDocTypesJson").val(localDocTypesJson);
}
HOWEVER, the working code that was shared with me (that was written ages ago by some employee who's not here anymore). Has the following URL when I use the autohotkey:
https://<COMPANYWEBSITE>/DocumentSearch.do?searchType=all&localDocTypesJson=7D&formAction=search&formInitialized=true&searchResultsView=default&btn_search=Search&docName=*<CLIPBOARD>*&wildcards=on&docRevision=&latestRevOnly=true&docProjectNumber=&docEngChangeOrder=&docLocation=&findLimit=500&docTypes=Customer+Drawing&docTypes=Production+Drawing&docTypes=Manufacturing+Process+Document&docTypes=Specification+Or+Standard
Note: replaced text with "CLIPBOARD" for clarification.
I was wondering if that's a type of "URL-programming" or how can I make a direct URL that prompts for the search results from the website? is that Javascript? or how is that programmed? (I know Swift and some Java, but have never really used Javascript).
It doesn't seem like you are asking an AutoHotKey (AHK) question, but to give you an AHK example you can copy, here is how I would use AHK to use Google.com to search for whatever is in my clipboard:
wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := true
wb.Navigate("https://www.google.com/search?q=" . StrReplace(Clipboard, " ", "+") . "", "")
Note, the URL format includes the query ("?q=whatever+you+had+in+Clipboard") in it with spaces replaced by "+"s.
Hth,

Trouble Adding Array output to an Dynamically Generated HTML String in GAS Google Script

I am trying to automate my businesses blog. I want to create a dynamic html string to use as a wordpress blog description. I am pulling text data from email body's in my gmail account to use as information. I parse the email body using the first function below.
I have everything working properly except for the for loop (in the second code block) creating the description of the post. I have searched for hours and tried dozens of different techniques but I cant figure it out for the life of me.
Here is how I am reading the text values into an array:
function getMatches(string, regex, index) {
index || (index = 1); // default to the first capturing group
var matches = [];
var match;
while (match = regex.exec(string)) {
matches.push(match[index]);
}
return matches;
}
This is how I am trying to dynamically output the text arrays to create a basic HTML blogpost description (which I pass to xmlrpc to post):
var1 = getMatches(string, regex expression, 1);
var2 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var fulldesc = "<center>";
var text = "";
for (var k=0; k<var1.length; k++) {
text = "<u><b>Var 1:</u></b> " + var1[k] + ", <u><b>Var 2:</u></b> " + var2[k] + ", <u><b>Var 3:</u></b> " + var3[k] + ", <u><b>Var 4:</u></b> " + var4[k] + ", <br><br>";
fulldesc += text;
}
fulldesc += "</center>";
Lastly here is the blog post description code (using GAS XMLRPC library):
var fullBlog = "<b><u>Headline:</u> " + sub + "</b><br><br>" + fulldesc + "<br><br>General Description: " + desc;
var blogPost = {
post_type: 'post',
post_status: 'publish', // Set to draft or publish
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
categories: cat, //cat is defined elsewhere and works fine
date_created_gmt: pubdate2, //defined elsewhere (not working but thats another topic)
mt_allow_comments: 'closed',
description: fullBlog
};
request.addParam(blogPost);
If there's only one value in the var1,2,3,4 arrays all works as it should. But any more than 1 value and I get no output at all from the "fulldesc" var. All other text variables work as they should and the blog still gets posted (just minus some very important information). I'm pretty sure the problem lies in my for loop which adds the HTML description to text var.
Any suggestions would be greatly appreciated, I'm burned out trying to get the answer! I am a self taught programmer (just from reading this forum) so please go easy on me if I missed something stupid :)
Figured it out: It wasnt the html/text loop at all. My blogpost title had to be a variable or text, but not both.
Not working:
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
Working:
var test = 'Ticker: ' + sub;
//
title:test,

How do I set my HTML5 datetime-local input field to the current local date-time in Angular?

I browsed through a few old questions here but most of them are about Angular 2 beta/RC versions which didn't ship with the date pipe.
I got closer with THIS answer from Dec 2015 which suggested the following solution:
<input type="datetime-local" [ngModel]="datetime" (ngModelChange)="datetime = $event" name="datetime">
constructor() {
this.datetime = new Date().toISOString().slice(0, 16);
}
This almost worked correctly as it outputs the current datetime as: 02-06-2017 05:02 AM where the date is correct but not the time. The local datetime here is 02-06-2017 10:48 AM.
I haven't configured the default timezone anywhere in Angular. Need I do that or is there a better solution to this problem? Also, it might be possible to implement the built in date pipe but I'm not sure how.
Here is a plnkr demo
To show current local date-time you just have to simply create an ISO like string as follows:
this.datetime = this.datetime.getFullYear() + '-' +
("0" + (this.datetime.getMonth() + 1)).slice(-2)
+ '-' + ("0" + this.datetime.getDate()).slice(-2)
+ 'T' + this.datetime.getHours() + ':' + this.datetime.getMinutes();
Feel free to comment and let me know if you have any problems.

splite string using mutilple characters

what I need is simple thing, I have string which cantains data - time retrived from mySQL in mySQL format HH:MM:SS YY-MM-DD what I need is to split this string in actionscript to array of numbers like this
HH
MM
SS
YY
MM
DD
so I can compare it with current time, any one know how to splite using multiple delimiters at first, then compare it with current time. this is my work until now
var param:Array = datetime.split(" :-");
var currentTime:Date = new Date();
var seconds:uint = currentTime.getSeconds();
var minutes:uint = currentTime.getMinutes();
var hours:uint = currentTime.getHours();
var days:uint = currentTime.getDay();
var monthes:uint = currentTime.getMonth();
var years:uint = currentTime.getFullYear();
if(int(param[3]) > years)
return years + " سنة ";
if(int(param[4]) > monthes)
return monthes + " شهر ";
if(int(param[5]) > days)
return days + " يوم ";
if(int(param[0]) > hours)
return hours + " ساعة ";
if(int(param[1]) > minutes)
return minutes + " دقيقة ";
if(int(param[2]) > seconds)
return seconds + " ثانية ";
return param[0] + " يوم ";
`
Split allows the delimiter to be a regexp, so you can say this or that. Something like this:
myStr.split(/:|-/)
Good luck!
You can use multiple characters, with separated by pipe |
myStr.split(/:|-|[|\(|\)]/)
make sure to use \ if you use ( ) and similar, so ( )
To solve your specific question, you might consider replacing the characters to be the same, and then split on that one. Use datetime.replace(/[ :-]/g, "|") and then split on "|". (I didn't check the correctness of the regexp). What Tyler says is more elegant: datetime.split(/[ -:]/). I stand for the rest though:
What MySQL outputs (via php?) is a standard date notation. You could try and use the Date.parse(dateString) to get a timestamp from it, and convert that into a date object by passing it as the sole constructor parameter:
recordedTime = new Date(Date.parse(datetime));
You could then compare the two date objects directly.
if (recordedTime.getFullYear() > currentTime.getFullYear()) { ... }
Hope it helps.