I have a DateTime value stored in an SQL Server DB with a value like '2016-04-13 00:00:00.0000000'. My problem is that I want to display this value on a HTML page as is without the browser or my server adjusting for Time Zone offsets.
I make an Ajax request to get my data to an MVC controller. I can see that the value of the Date in my ViewModel is correct. When it gets to the browser depending on the machines timezone the Date is offset.
All I want is that if I have a value 2015-04-13 00:00 in my database then that exact value gets displayed on my interface regardless of timezones, locales etc.
My Ajax request returns a value like "/Date(1460502000000)/". I've tried the following conversions:
new Date(datavar);
Date(datevar)
new Date(parseInt(datevar.replace('/Date(', '')))
I've also tried using moment.js like the following:
moment.utc(datevar).format();
moment(datevar).tostring()
An several others all of which try to offset the date some way or other.
The suggested linked answer is different in that it will include the timezone offset. So a client on US Central Time a value of "/Date(1460505600000)/" will be converted to 'Tue Apr 12 2016 18:00 GMT-0500" while a client on GMT will get the same value converted to "Wed Apr 13 2016 00:00:00 GMT+0100"
Related
I am using the DateTime-local in the HTML field to obtain the datetime from the users. I am sending the same to the backend which I am using to create the XML file. When I send the date to backend it subtracts itself by 2 hours for some reason.
I am not making any changes to it. I am currently in Central European Time (CET), I am guessing its changing automatically to UTC time because CET is 2 hours ahead of UTC. I am just curious to know whats happening here.
I am using the HTML, AngularJS and Nodejs for my project and following are my code samples:
HTML:
<input type="datetime-local" class="form-control" ng-model="formdata.EventTime">
For example, if I selected: 08 October 2020 12:30 PM then after the selection the field would appear something like this: 08/10/2020 12:30
In the AngularJS, just before making the HTTP POST request to my NODE.js I tried to console.log the time and it appeared something like this:
Thu Oct 08 2020 12:30:00 GMT+0200 (Central European Summer Time)
Now finally when I console.log in my Node.js then it appears something like this:
2020-10-08T10:30:00.000Z
I am just curious to know if this is some default functionality?
I could not find similar questions elsewhere so I am posting this.
There's a simple enough reason for the difference. The outputs of both are showing the same instant in time, however the AngularJS log is formatting the date in RFC 2822 format which shows the local timezone, while Node.js is logging the time in UTC formatted as an ISO 8601 time.
If you do this in both AngularJS and Node.js:
console.log(date.toISOString());
You'll get the same output (e.g.)
2020-10-08T10:30:00.000Z
Likewise if you try
console.log(date.toString());
You should get a similar output but in RFC 2822 format (e.g.)
Thu Oct 08 2020 12:30:00 GMT+0200 (Central European Summer Time)
Thanks, #Terry for the response. Based on his answer I did the following, I used the moment to convert my time from UTC to local. My project required me to retain the format of the date and syntax of it.
Install the moment library in Node.js.
When I was selecting 08/10/2020 15:30 in the datetime-local I was getting following date in Node.js:
2020-10-08T13:30:00.000Z
I tried converting it using:
EventTime = moment.utc(EventTime).local().format('YYYY-MM-DDTHH:mm:SS.sss');
Finally I got following output:
2020-10-08T15:30:00.000
I hope if someone else has the same doubt this might help.
I am working on a webrequest, and the last part of it is (for exemple) :
...&cb=jQuery1702547129448580113_1468872275121
I was sure this relies to the DateTime the Get request is send to the server, and 1468872275121 indeed is 18/07/2016 20:04:35 UTC (converted from Json/Unix date format).
Ok good.
But any idea what could be the first part ?? Is it also a DateTime value ? (the request is sent when adding some item to your cart on a commercial site)
Thanks!
The default for dates in JSON is a string in RFC 3339 or IS 8601 format, like "2012-04-23T18:25:43.511Z". You will also find dates stored as a number storing either seconds or milliseconds since midnight, Jan 1st. 1970 in UTC.
A year has about 30 million seconds. Compare that with the number that you see.
In my json date is represented like this :-
"from":"2015-11-11T09:21:00.00Z"
But when it gets converted to java.sql.Timestamp it looks like this :-
2015-11-11 17:21:00.0
My timezone is Singapore . It is 8 hours ahead of UTC timezone and coincidentally the date also gets converted to 8 hours ahead of its time.
They are showing the same times, just formatted differently for the different locations. The time you are taking in is UTC/GMT. What you are viewing in your IDE is showing the local time stamp formating, but they are the same values and points in time.
If it really matters how it is displayed to you in the debugger you can use a Calendar object instead of a TimeStamp and set the locale value to be UTC and it will format them the same way, but again they are the same values.
I don't want the Timestamp in java to be different from json
It is not different, the display format is different do to the location setting but they are the same values just represented differently.
P.S. Be aware that if your server is set to a different time zone than your work station it will show a different format as well, but it again will be the same time just represented differently.
I have a PHP-MySQL API with Time Zone Support. My database is in UTC and all date time calculations are made in UTC time, just converting to local time when data is displayed to the user.
The problem is when trying to retrive data in a natural time interval for a user in a different time zone, for example weeks, months, years and so on. The DB column is type DateTime and store dates in UTC. If I need all rows grouped by month and just use the UTC date stored in the database I will get some wrong rows, if the Time Zone differences made some rows shift its month.
Example: row with value 2015-05-01 00:00:00 is in May in UTC, but should be in April for any user in a negative Time Zone.
So using UTC is not the solution here. I need first to convert those dates to client Time Zone.
Which aproach can I use to overcome this problem?
As an example this is view groups some data by week:
SELECT `individuals_id`,
Str_to_date(Concat(Yearweek(`sessions_date`, 5), ' Monday'), '%X%V %W') AS `sessions_date`,
`protocol_index`,
`zone_index`,
Avg(`asymmetry_dom`) AS `asymmetry_dom`,
Avg(`asymmetry_rl`) AS `asymmetry_rl`,
FROM `sessions_data_view`
GROUP BY `individuals_id`,
Yearweek(`sessions_date`, 5),
`protocol_index`,
`zone_index`
ORDER BY `individuals_id`,
Yearweek(`sessions_date`, 5),
`protocol_index`,
`zone_index`
The problem is that Yearweek() should have different output for a row depending on the user time zone. It is not posible to use the column sessions_date in UTC if want to give consistent result to the user.
Rigth now I do not know the user Time Zone, but this should not be a limitation, since the app is in its desing phase and anything can be changed.
The API is a PHP application getting HTTP requests. It talks to a PHP Database class that wraps all queries to the MariaDB database. All response from the aPI is given as JSON, dates formated as UTC strings. The data is shown via a web application. DateTime Javascript objects are responsible to convert the responses from the API to correct dates for the client time zone.
Firstly, the only reliable way you have of getting the client's timezone is through javascript, assuming their computer's time-settings are correct.
Solution One - Javascript to PHP
I suggest collecting the users timezone with the following javascript:
var timeZone = new Date().getTimezoneOffset();
You will need to store this value in the database. You then format the UTC time with PHP code similar to this:
$date = new DateTime();
$timezone = new DateTimeZone('Australia/Sydney');
$date->setTimezone($timezone);
$formatted_date = $date->format('Y-m-d H:i:s');
Solution Two - PHP to Javascript
You convert a UTC string into a unix timestamp with the following:
$time = strtotime($utc);
You then return that to the browser, and have javascript format it like this:
var date = new Date(timestamp * 1000),
datevalues = [
date.getFullYear(),
date.getMonth()+1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
];
Good luck!
edit: For converting MySQL for use with queries, use CONVERT_TZ, the format being:
CONVERT_TZ('date/time field','starting timezone','ending timezone')
For example:
select *, DATE_FORMAT(CONVERT_TZ(str_to_date(timestamp),'UTC','US/Eastern'), '%m/%d/%y %h:%i %p') as 'Date - Eastern' FROM table`
I am trying to build a calculated field for a web database in Microsoft Access 2010.
The field should show the day in a three character format as follows:
Mon
Tue
Wed
from an existing date column in the table.
Obviously the Format function is not compatible with the web. And the following function FormatDateTime does not have the option to convert to a string day, just a long and short date and time.
Any ideas on how this can be accomplished?
If this is not possible my only option seems to be to create a client object.
I have tried adding the function into a query instead but this is also incompatible with a web database.
WeekdayName should suit. Be careful with FirstDayofWeek:
SELECT WeekdayName(Weekday([ADate],1),-1,1) FROM Table
Syntax:
Weekday(Date,[FirstDayOfWeek])
WeekdayName(Weekday as Long,[Abbreviate as Boolean],[FirstDayOfWeek])
1 = Sunday in both of the functions.