JSON date in javascript not works - javascript

I have a MVC web site, in some views I show a datetime from JSON value, frist I show month in one input/text control and the year in other control. The website is used by many cities and works fine, but in one city when I convert JSON date value to Javascript date value, date value has one month less, for example if JSON date is 2016-04-01 12:00:00 Javascript date value is 2016-03-01, I think is a configuration of the PC is this city but what configuration?
This is the code:
var Ffab = new Date(parseInt(item.Ffab_ettc.replace(/\/Date\((.*?)\)\//gi, "$1")));
$("#txtFfabTCM").val(Ffab.getMonth() + 1);
$("#txtFfabTCY").val(Ffab.getFullYear());
item.Ffab_ettc is the JSON Datetime value.

Related

What is the data type for date and time in PostgreSQL?

I know the data types are TIMESTAMP, DATE, TIME, etc. I also know I should be using TIMESTAMP as it keeps track of any changes.
I am bringing the date from the front-end where in my react component the date is
const dateTime = new Date().toLocaleString("en-GB")
The output of which is "24/10/2021, 14:37:49"
If I set the data type to TIMESTAMP it gives me an error.
The question is what should be the data type to accept the above mentioned date and time format.
I have tried DATETIME but it gives an error saying DATETIME does not exist.
SQL query:
ALTER TABLE user_info_2 ADD date_col1 TIMEDATE;
I am new at PostgreSQL so any help is appreciated.
The issue is not the format per se, it is that the default Postgres Datestyle date ordering is:
--Note the MDY date ordering
show datestyle ;
DateStyle
-----------
ISO, MDY
select '24/10/2021, 14:37:49'::timestamptz;
ERROR: date/time field value out of range: "24/10/2021, 14:37:49"
LINE 1: select '24/10/2021, 14:37:49'::timestamptz;
---Change date ordering to put date first
set datestyle = iso,DMY;
show datestyle ;
DateStyle
-----------
ISO, DMY
(1 row)
select '24/10/2021, 14:37:49'::timestamptz;
timestamptz
------------------------
2021-10-24 14:37:49-07
So you have two choices:
Change the DateStyle 'permanently' in postgresql.conf or temporarily per session using set datestyle = iso,DMY;
Use the ISO format:
select '2021-10-24, 14:37:49'::timestamptz;
timestamptz
------------------------
2021-10-24 14:37:49-07
I would say 2) is the preferred choice as Postgres does not store the formatting so it really does matter how you input the data. If the date ordering is important for your users then do the appropriate formatting on presenting the output to them.
As I could see, using Date().toLocaleString() will return a string type. You should use a Date object to be persisted. For your column type in Postgres use TIMESTAMP.
typeof(Date().toLocaleString("en-GB"))
The output will be 'string'...

Birthdate off by one date

I have a javascript app in which the user selects their birthdate using a 3rd party datepicker.
This date is then sent to the server, where it is stored in SqlServer as a Date (not a DateTime, nor a DateTimeOffset).
This typically works fine. However, if the user is in a timezone such as +2:00, and it's just after midnight, then the date which is sent to the server is now different.
For example, I select the date of Jan 1, 2000 in the DatePicker.
The value which is being sent to the server is: 1999-12-31T22:00:00.000Z
The server then strips the time off it to store it as a date, and then the date is now off by one.
How do I resolve this?
I believe that dateFormat: "d/MM/yyyy" option of datepicker is what you're looking for.
Otherwise you can use the following code to convert Date object into the required string.
date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()
Both of the options will give you a string with the date, which you can then convert to date object in sql using str_to_date
See: https://stackoverflow.com/a/1861519/2652134

DateTime format on server

I have multi select check-box to select report dates. User is choosing one or more report dates. I am using JavaScript to get selected dates:
var dateTime = new Date($(this).val()).toJSON();
I am passing selected dates to controller and creading query:
foreach (var reportDate in reportDates)
{
predicateReportDate = predicateReportDate.Or(p => p.LastUpdateDate == reportDate.ReportDate);
}
Everything works fine locally but I found issue with date format when I deploy page to server.
Locally DateTime format is like
'2017-04-21 00:00:00'
but on server I see totally different date:
4/20/2017 6:00:00 PM
It causes that filter does not work properly on server (no results are returned) because there is no report with that date.
Both:
LastUpdateDate
and
reportDate.ReportDate
are DateTimes (not strings)
Can you please help me with unifying date format?
What I did is:
I replaced line which reads date from filter:
var dateTime = new Date($(this).val()).toJSON();
with
var dateTime = $(this).val().split(' ')[0];
It seems that toJSON() is converting date (I am using UTC) to local server timezone.

Working with date formats in Java and MySQL

Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed

Is there a way to get only month or only year from dateTime objects which are sent using json from database to javascript?

I have a table containing 2 dateTime columns 'start date' and 'Endate' ,I have passed the entire table to javascript using json,the format of data which I get when I access a particular date in the json table is dd/mm/yyyy (along with the default time),I need to get only date i.e dd and only month i.e mm from this format in javascript
Is there a way to do this ?
Date: the_string.split('/')[0]
Month: the_string.split('/')[1]
you can use regex to parse the data.. let me know if you need help in this with your sample content.

Categories