node js Sequelize with MySQL date formate issue - mysql

I developed node js services with Sequelize with MySQL Everything working good. I am storing data in DB with date and time with service but if I get the same data with service, node js automatically converting time (a wrong format which is not in the database), I want same time which is present in the database. to solve this issue I set the timezone in services and in the database but none of them is working. can anyone provide me with the solution.

suggestion
you could include time format function in mysql
for example
SELECT TIME_FORMAT("19:30:10", "%H %i %s");
result
TIME_FORMAT("19:30:10", "%H %i %s")
19 30 10
ref

That's not the issue of node js , Sequelize by default gives the time in UTC , so that date time will be correct
Check : Just compare your date time with UTC , and check is that you are getting.
Solution : So what you can do is , just convert returned date and time to your timezone and you are good to go.
And that's a good practice if you are using UTC everywhere , you can get benefits when you are getting / showing in many countries.

Related

Lumen show different time than what is saved is MYSQL DB

my current timezone is Asia/Karachi and when i retrieve table data from mysql it gives me (actual time - 5 hours)
for eg:
mysql column value: '2021-04-21 01:34:57'
and when i retrieve from laravel DB::table('table_name')->get()->toArray();
it gives following :2021-04-20 20:34:57
and changing my timezone doesn't change anything either.
so is there something else i'm missing ?
btw i created following route for checking my current timezone
$app->get('/timezone', function () {
return date_default_timezone_get();
});
and it gives same what is saved in my env i.e(Asia/Karachi) but this doesn't change the result i get from mysql even if i change it to some other timezone like Asia/Kolkata.
I tried researching on this but didn't get any suitable answer.
i tried alot of things to solve this issue like
adding env variables APP_TIMEZONE="Asia/Karachi" (did not work)
also i tried adding DB_TIMEZONE="+05:00" (this worked in my local but not on stagging)
finally i saw somewhere that someone else had exact same issue resolving this and he did it by adding the hours using carbon which also worked for me
'posted_at' => Carbon::parse($record->posted_at)->addHours(5)->toDateTimeString(),
i know this is not the optimal solution but this was the only solution that worked so i had to go with it.
https://dev.mysql.com/doc/refman/8.0/en/datetime.html
If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.
As far as I know, you have a couple things to consider:
The timezone of your Laravel/Lumen app
This can be found found in config/app.php -> timezone.
The timezone of your MySQL server
You can retrieve this using:
mysql> SELECT ##global.time_zone, ##session.time_zone;
The timezone of the server itself
If the mySQL query above returns SYSTEM, it means it uses the system timezone setting, which for Debian/Ubuntu etc you can check using:
cat /etc/timezone
From my experience, you can most often leave the system/mySQL timezones intact and only set the correct timezone in your Laravel config. I know it caused me a headache the first time I had to figure out how this actually worked.

how to find out the timezone of given time from mysql db in node js

I am quite new to Node js.
I want to know what timezone the given datetime value, which comes from mysql db, it is .
For example
const conference = await mysqlHelper.query(conferenceSql);
// select query
const time = conference[0].created_datetime
// I wonder how to get the timezone from this time variable here.
console.log(time)
// console.log returns '2021-02-23T01:30:00.000Z' to the terminal
mysql value type: datetime
I know I could directly look up the db table instead of figuring out in node js. However, this is a required task for validation in my server.
Thanks a lot !!
Mysql datetime does not store time zone information.
2021-02-23T01:30:00.000Z is displayed in an ISO 8601 format, being in UTC.

MySql Timezone JDBC issue

I am trying to insert a date value in MySql table name person and column name regdate with data type = datetime. I am setting a value e.g. '2019-08-21 20:25:20' but after saving +5:30 hours get added and value which gets stored is '2019-08-22 03:55:20'. Generating the date value using below Java code
Timestamp curDate = Timestamp.valueOf(Instant.now().atZone(ZoneId.of("Asia/Kolkata")).toLocalDateTime());
and then using .setTimestamp(1, curdate); in INSERT query.
I have checked that the timezone of MySql is set to IST (GMT+0530). App Server timezone is also set to IST. But I am not able to understand why +5:30 hours are getting added even if I explictly setting the date value.
I have tried setting timezone in connection string as ?serverTimezone=Asia/Kolkata but didn't work.
But if I run the same code using my local machine connecting same MySql instance, I get no problem and same value gets stored without addition of 5:30 hours. I checked App Server timezone and it is IST.
MySql version - 5.7.17-log
mysql-connector-java - 8.0.15
Am I missing something?
You have a few problems here.
Avoid legacy date-time classes
First of all, you are mixing the terrible legacy date-time classes (java.sql.Timestamp) with the modern java.time classes. Don’t. Use only classes from the java.time packages.
LocalDateTime cannot represent a moment
You are using LocalDateTime to track a moment, which it cannot. By definition, that class does not represent a point on the time line. That class has a date and a time-of-day but intentionally lacks the context of a time zone or offset-from-UTC. Calling toLocalDateTime strips away vital information about zone/offset.
Tools lie
You are likely getting confused by the well-intentioned but unfortunate behavior of many tools to dynamically apply a time zone while generating text to represent the date-time value retrieved from the database. You can avoid this by using Java & JDBC to get the pure unadulterated value from the database.
TIMESTAMP WITH TIME ZONE
You failed to disclose the exact data type of your column in your database. If you are trying to track a moment, use a data type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE. In MySQL 8 that would, apparently, be the TIMESTAMP type according to this doc. (I am a Postgres guy, not a MySQL user.)
In JDBC 4.2 and later, we can exchange java.time objects with the database. So no need to over touch java.sql.Timestamp again.
Unfortunately, the JDBC spec oddly chose to not require support for Instant (a moment in UTC) nor forZonedDateTime(a moment as seen in some particular time zone). The spec does require support for [OffsetDateTime`]2.
Tip: Learn to work in UTC for the most part. Adjust into a time zone only when required by business logic or for presentation to the user.
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ; // Capture current moment in UTC.
Write to the database via a prepared statement.
myPreparedStatement.setObject( … , odt ) ;
Retrieval.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
View that moment through the wall-clock time used by the people of some particular region (a time zone).
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

Converting seconds to Datetime in mysql

I am using JSP Servlets and have made a MySQL connection.
I have a value in seconds, which is basically the value of validity of a token.
I now want to convert this value and insert in Datetime format in MySQL.
So, bascially it should be something like
Datetime( Current Time + expiry_value_in_seconds )
How can I do this?
FoggyDay already answered how you can do it in Java, so I'll answer how to do it in MySQL specific SQL itself:
ADDDATE(current_timestamp, INTERVAL 10 second)
If you want to do it on the Java side, I'd use Calendar:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, expiry_value_in_seconds);
Here is a great tutorial:
Lars Vogel:
Java Date, Calendar and Time APIs
Otherwise, I'd use mySql DATE_ADD():
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
http://www.java2s.com/Tutorial/MySQL/0280__Date-Time-Functions/DATEADDcurdateINTERVAL1SECOND.htm

The Generation Time is wrong when pint in phpMyAdmin

I using phpMyAdmin 4.4.14 in Win7+Chrome and MySQL 5.6 in Linux.
My timezone is +8
The date command in Linux returns a correct date and time.
When I issue select now() inside the phpMyAdmin, the date and time is correct.
But, when I print the result, the time value in the Generation Time is wrong.
Look like that the Generation Time does not do a +8 to the hour.
How to fix ?
Cheers,
Alvin SIU
Print view is done via PHP script, so the issue is in PHP, not in MySQL. In order to change this timestamp, you need to open php.ini and to change/add date.timezone variable with desired value:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Paris"
All available timezones can be found here:
http://www.php.net/manual/en/timezones.php