Can't compare dates in Angular 2+ - html

I have 2 dates that I need to do some logic. The first date comes from SQL database and the second date is the current date that from angular.
I would like to display them in an HTML table if the SQL date is earlier, apply css stylebehind and later, apply ahead css style.
my current date that I use in component.ts is:
today: any = Date.now();
And my HTML table that I use ngIf is:
<td *ngIf="p.serverDate >= today" class="ahead">{{ p.serverDate | date }}</td>
<td *ngIf="p.serverDate < today" class="behind">{{ p.serverDate | date }}</td>
p is the object that returned from database that has date in it. I am ppretty sure that I have the serverDate and today. I can use date pipe on both of them. Why can't I use the simple if statement in here?
UPDATE: I've just found out that
serverDate is 2018-07-24T20:34:48 and today is 1533235371115 format.

Might be date from MySQL DB might be in string. You need to convert using new Date().
For any date manipulation, you can check moment.js library or date-fns library. For comparison b/w dates use isBefore() method in https://momentjs.com/docs/.

Related

How can I format the date from mysql (I'm using node.js)?

I'm using node.js to select info from my mysql database and I have a datetime column, but I'm not getting the format I want.
This is my sql code
SELECT id, data, titulo, subtitulo, texto, DATE(datahora_cadastro) as data FROM sig_noticias
I need the data to be like this: DD/MM/YYYY
But I'm getting this: Thu Mar 30 2017 00:00:00 GMT-0300 (GMT-03:00)
Easiest way to format dates in Javascript is by far using a modern date / time library. I personally use MomentJS and it works well. I highly recommend this solution if you plan on working with dates in even just a few areas of your app.
Using moment, you could simply call moment(myDate).format("DD/MM/YYYY");.
If you really want to stick to pure Javascript, however, here's how to get the format you want:
const formattedDate = `${myDate.getDate() + 1}/${myDate.getMonth() + 1}/${myDate.getFullYear()}`;
Javascript's built-in date functions are pretty confusing. Here, for instance, the getDate and getMonth methods return the index of the date / month, and not the actual date / month, so we need to add +1 to those.
Using vanilla Javascript then becomes even harder when dealing with date manipulation (adding, subtracting) and timezones.

How to check if a string contains a date?

I'm trying to iterate on a DataSet, this contain a results of query such as SELECT * FROM tb 1, now the first three field contains a date, the format saved in the database table is this:
yyyy-MM-dd HH:mm:ss
but the code return this:
yyyy/MM/dd HH:mm:ss
in particular this:
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Next
So I need to recognize if the current string have this format: yyyy/MM/dd HH:mm:ss and parse it into: yyyy-MM-dd HH:mm:ss I tough to a regex pattern for recognize it, but I'm not an expert of regex. Anyway, if there is another solution I'll glad to see. Note that only the first three value and the last one of the table is date, the other values aren't date but contain integer or other string value.
Dates do not have a format. From MSDN:
Represents an instant in time, typically expressed as a date and time of day.
...
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar...For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
So, a DateTime is just a Big Number. Representing them as "dd/MM/yyyy" is part of the magic of the DateTime type. Part of the issue is this:
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Row items are Object. It wont act like a DateTime type unless/until you get it into a DateTime variable. That print as a DateTime simple because the DataTable knows the underlying type; but it will use the default format for your Culture. This makes it look like dates have a built in format (or even that the "format changed" if you tried to set it to something), but you are a human and 635882810022222112L would not make sense to most of us.
To change the output style, you first need to get it into a DateTime variable. Apparently, a preliminary step is to determine if an arbitrary column is a Date. Rather than testing the "format" of the output, test the underlying data type. This does assume a proper DateTime column in the DataTable:
If ds.Tables(0).Columns(n).DataType = GetType(DateTime) Then
'...
End If
' Or:
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
'...
End If
Then to change the display, first get it into a DateTime variable:
Dim dt As DateTime
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
dt = Convert.ToDateTime(ds.Tables(0).Rows(x)(z))
' cant change "format" but you can change how it displays:
Console.WriteLine(dt.ToLongDateString)
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm tt"))
Console.WriteLine(dt.ToString("dd MMM, yyyy"))
End If
An easier way to get and convert to DateTime is to use the Field(Of T) extension:
Dim dt = ds.Tables(0).Rows(x).Field(Of DateTime)(y)
when I peform the insert usually do this: Date.Now.ToString("yyyy-MM-dd HH:mm:ss") so I apply a format to date to insert... if I don't format correctly the date as I shown I get this value 0000-00-00 00:00:00
That doesn't apply a format to a date. It converts the DateTime to a string. While "yyyy-MM-dd HH:mm:ss" is the correct format to use when passing date data as a string to MySql, it is not needed. The MySQL Data provider knows how to convert a Net DateTime var to the data MySql needs/wants and back again -- that's its job.
' this will work fine
cmd.Parameters.Add("#SomeDate", MySqlDbType.DateTime).Value = myDateTimeVar
The format requirement you read about is the what you need to use in the MySql shell or WorkBench UI because you are entering text/string there...from the keyboard. It does not mean code must convert DateTime variables to string in a specific format for storing.
I ended up using this
Try
Dim theDate As DateTime = dr.Item(colName)
Return theDate
Catch
' do something
End Try
I would be happy to see a better method.
Based off of what you seem to be asking a simple replace would do
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString().Replace("/","-"))
Next
if it comes in with / they are changed to - if it comes in with - they remain intact.
Depending on the flexibility you want in this, it may be necessary to TryParse to ensure that the value you're working with is actually a valid datetime.

VB.net - Get date from MySQL, add it with a month and compare it with today's date

I'm making an invoice system using VB.Net and MySQL.
First of all, I have to retrieve a date from mysql, add it with a month and compare it with today's date.
My date format in mysql is "yyyy-MM-dd"
My problems are:
How should I add a month to the date which I get from mysql?
How should I check if that date have already exceed?
First Get the data from MYSQL
Dim dateFromMySQL as DateTime = //Get from MYSQL
Then
1) How should I add a month to the date which I get from mysql?
Just use AddMonths to add month to date.
Dim newDate as DateTime = dateFromMySQL.AddMonths(1)
2) How should I check if that date have already exceed?
if dateFromMySQL <= DateTime.Now Then
Look, I don't know vb.net but as it uses .Net and I know C# it's probably the same.
If you're not using an ORM, you'll have to get the date representation from MySql, convert it to a Date or DateTime object using the class constructor, and this object has methods to add a month and compare to a limit date.
I think it'll solve your problem.

How to get the date value in the table in specific format?

I have one problem in my tool. I am using one MySQL table called calendar. From this table I am fetching some date values. The format of date in calendar table is yyyy-mm-dd. But in my system having dd-mmm-yy. In my c# application I fetched the date from the Calendar and If I displayed the date in c# application means date format(2012-10-05) changed to system format(05-oct-12). But I need to display in yyyy-mm-dd format. I don't know how to solve this problem.
To display DateTime in specific format, you can specify the format in .ToString() method like:
DateTime dt = DateTime.Now; //your DateTime variable
Console.WriteLine(dt.ToString("yyyy-mm-ddd"));
You may see the article: Standard Date and Time Format Strings - MSDN

Informix iSQL date function help

I'm trying to extract just the year out of either of the built-in date functions of Informix
TODAY or CURRENT
Is there a way to do this without SUBSTR()?
Yes: use the YEAR function:
SELECT YEAR(TODAY)
FROM SysMaster::SysDual;
Similarly for MONTH, DAY, WEEKDAY (0..6). You could use CURRENT instead of TODAY, but that generates a DATETIME value which is then converted to DATE and then analyzed.