To use the user time zone, date and time format¶
Items
- Prerequisites
- To save date and time data sent from the screen in DB
- To save the date data sent from the screen in DB
- To display the date and time data which is saved in DB on user screen
- To display date saved in DB on user screen
- To create dates for 3 days of user time zone starting from today at the client side and sending the last date to the server side
Prerequisites¶
- Various Setting Values
Set Item Setting Value User Time zone (GMT+09:00) Japan/Tokyo System Time zone (GMT+00:00) UTC Date and time format English Format Date (Standard Display) MMM d, yyyy Date (Simplified Display) MMM d Date (Input) yyyy/MM/dd Time (Standard Display) h:mm a Time (Timestamp Display) h:mm:ss a Time (Input) | HH:mm
Table Definition
CREATE TABLE example_table ( user_cd VARCHAR(100) NOT NULL, update_date TIMESTAMP NOT NULL, PRIMARY KEY (user_cd) );
To save date and time data sent from the screen in DB¶
The flow of analyzing the date and time entered from the screen, at the server side and saving it in DB is explained here.It is assumed that the user has entered the following date time data and sent to the server.2012/09/19 03:46
1. To analyze the date and time string sent from screen¶
function init(request) { var inputDateStr = request.inputDate; //Date and time string sent from screen var formatIds = { AccountDateTimeFormatter.IM_DATETIME_FORMAT_DATE_INPUT, AccountDateTimeFormatter.IM_DATETIME_FORMAT_TIME_INPUT }; var inputDate = AccountDateTimeFormatter.parseToDate(inputDate, formatIds); }Use AccountDateTimeFormatter, when date and time string is not according to the date and time input format.AccountDateTimeFormatter analyses using time zone of logged in user.
2. To save date and time data in DB¶
var db = new TenantDatabase(); var userCd = Contexts.getAccountContext().userCd; Transaction.begin(function() { db.execute('INSERT INTO example_table values(?, ?)', [DbParameter.string(userCd), DbParameter.timestamp(inputDate)]); });Save date and time data which is changed to the system default time zone in DB.Since Date type object does not have time zone, date and time data which is changed to the time zone of Java VM is saved in TIMESTAMP type column of DB.Java VM time zone and system default time zone are the same.
To save the date data sent from the screen in DB¶
The flow of analyzing the date data entered from the screen at the server side up to saving it in DB is explained.Warning
Even though only the date is entered from the screen, generally time should also be considered; as given in the below example.(Example) The deadline of transportation-expenses applicationThe meaning of “Deadline” means, the time up to 23:59:59 (Or, business hours).For example, if the deadline is 19th Sep of Japan Head Office (GMT+09:00), transportation-expenses application should be finished by 5 P.M. of 19th Sep at the Honolulu branch office (GMT-10:00)Assume that the following value is entered in the date input format of the user and sent to the server.2012/09/19
1. To analyze date string sent from the screen¶
function init(request) { var inputDateStr = request.inputDate; //Date string sent from screen var inputDate = AccountDateTimeFormatter.parseToDate(inputDate, AccountDateTimeFormatter.IM_DATETIME_FORMAT_DATE_INPUT); }In case of date string, time part is considered as “00:00:00”.
2. To save date in DB¶
It is similar to “To save date and time data in DB”.
To display the date and time data which is saved in DB on user screen¶
The flow of adjusting the date and time which is saved in DB; to the date and time string, using date and time display format and user time zone up to displaying it on the screen is explained.
1. To fetch date and time data from DB¶
var db = new TenantDatabase(); var userCd = Contexts.getAccountContext().userCd; var result = db.select('SELECT update_date FROM example_table WHERE user_cd=?', [DbParameter.string(userCd)]); if (result.error) { return; } var outputDate = result.data[0];Date and time data which is changed to system default time zone is saved in DB.
2. To adjust date and time string¶
var formatIds = { AccountDateTimeFormatter.IM_DATETIME_FORMAT_DATE_STANDARD, AccountDateTimeFormatter.IM_DATETIME_FORMAT_TIME_STANDARD } var outputDateStr = AccountDateTimeFormatter.format(outputDate, formatIds);Use AccountDateTimeFormatter when adjusting to the display format of date and time of the user.AccountDateTimeFormatter calculates the time according to the time zone of the logged in user.Following result is obtained.Sep 19, 2012 3:46 AM
To display date saved in DB on user screen¶
The flow of adjusting the date which is saved in DB to the date string, using date display format and user time zone upto displaying it on the screen is explained.
1. To fetch date from DB¶
It is similar to “To fetch date and time data from the DB”.
2. Adjust to date string¶
var outputDateStr = AccountDateTimeFormatter.format(outputDate, AccountDateTimeFormatter.IM_DATETIME_FORMAT_DATE_STANDARD);It is similar to “To adjust date and time string”.Following result is obtained.Sep 19, 2012
To create dates for 3 days of user time zone starting from today at the client side and sending the last date to the server side¶
Understand the important points for handling the date and time data by csjs through this sample.
1. To fetch “Today” of user time zone¶
User time zone is the time zone registered in intra-mart Accel Platform by the user.Use ImDate provided by intra-mart Accel Platform to fetch the “Today” in user time zone by csjs.Warning
Do not use new Date of csjs for fetching “Today” in user time zone.newDate of csjs returns the current date and time data in time zone of client OS. However, user time zone may not necessarily match with the client OS time zone.<script type="text/javascript" src="im_i18n/timezone/im_date_timezone.js"></script> <script type="text/javascript"> var firstDate = ImDate.now(); </script>
2. To generate dates of 3 days starting from today¶
Method provided in Date as the standard can be used.var dateArray = new Array(); var date = firstDate; for (var i = 0; i < 3; i++) { dateArray[i] = date; date.setDate(date.getDate() + 1); }
3. To send latest date to the server¶
Create date string from date value.var lastDate = dateArray[2]; var lastDateStr = lastDate.getFullYear() + "-" + (lastDate.getMonth() + 1) + "-" + lastDate.getDate();Warning
Do not send Epoch milliseconds of Date created by ImDate.now().The Date which is returned by ImDate.now() has date time seconds of “Today” in user time zone. However, Epoch milliseconds are not necessarily right.The reason is that the Date is created at the client side.The value calculated according to the user time zone may not match with the Epoch milliseconds of Date which is calculated according to the time zone of the client time zone.Warning
Use DateTime when creating date and time data of user time zone at the server side. Do not use Date only.It is important to consider the time difference between user’s time zones to calculate the Date according to the system/default time zone.DateTime is calculated according to the user’s time zone if time zone is not specified.//Date and time data on 19th September 1996,3:47 in user timezone var dateTime = new DateTime(1996, DateTime.SEPTEMBER, 19, 3, 47, 0);
4. To create Date from date string sent from the client side¶
function init(request) { var inputDateStr = request.inputDate; //Date string sent from the screen var inputDate = DateTimeFormatter.parseToDate('yyyy-MM-dd', inputDateStr); }Use DateTimeFormatter, when date and time string is not according to the date and time input format of the user.DateTimeFormatter can specify the format pattern directly.DateTimeFormatter analyses using user time zone.