Am getting a GMT time from my server in this format
Fri, 18 Oct 2013 11:38:23 GMT
My requirement is to convert this time to local time using Javascript, eg:/ if the user is from India, first i need to take the time zone +5.30 and add that to my servertime and convert the time string to the following format
2013-10-18 16:37:06
I tried with following code but not working
var date = new Date('Fri, 18 Oct 2013 11:38:23 GMT');
date.toString();
Please help me to solve this issue, Thanks in advance
This worked for me:
<script>
var strDateTime = "Fri, 18 Oct 2013 11:38:23 GMT";
var myDate = new Date(strDateTime);
alert(myDate.toLocaleString());
</script>
Please take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp for all further date time manipulations, from the date object myDate.
Related
I have a list of dates (strings) from a delimited text file. All of these times are UTC. E.g 3 seconds past midnight on 1st July UTC
raw_time = 2022-07-01 00:00:03
I have converted this to a date using
my_time = new Date(raw_time)
I wish to test if the dates fall within a range
e.g.
Fri, 01 Jul 2022 00:00:00 GMT
to
Sun, 31 Jul 2022 23:59:59 GMT
The example fails because when I look at
my_time.toUTCString()
I get the result
Thu, 30 Jun 2022 23:00:03 GMT
I should add that I am in the UK on BST (GMT+1)
How can I force the raw date to be converted as a UTC date?
The problem is that your timestamps might be written in UTC timezone, but they are not valid UTC timestamps as per ISO standard: YYYY-MM-DDTHH:mm:ss.sssZ
var myDate = new Date("2022-07-01 00:00:03");
myDate.toUTCString()
//'Thu, 30 Jun 2022 23:00:03 GMT'
var utcDate = new Date("2022-07-01T00:00:03Z"); //note the Z character
utcDate.toUTCString();
//'Fri, 01 Jul 2022 00:00:03 GMT'
The best way to solve the issue would be to update your timestamps file with the correct format. If you for some reason can't, then you can modify the timestamp on the JS side by changing the string:
//raw_time is "2022-07-01 00:00:03"
const formattedTimestamp = raw_time.replace(' ','T').concat('Z')
// formattedTimestamp becomes "2022-07-01T00:00:03Z"
I guess the issue is because your local machine is in GMT Timezone, so when you do new Date(), it gives you Thu, 30 Jun 2022 23:00:03 GMT.
You can use moment timezone, where you can specify the timezone details like Canada, UK.
Please refer below:
https://momentjs.com/timezone/
Also you can refer to below link:
Momentjs: How to convert date/time of one timezone to UTC date/time
Hope this might help.
my js file looks like below
var today = Date();
console.log(today); // Thu Aug 04 2022 15:28:52 GMT+0530 (India Standard Time)
var today = new Date();
console.log(today); // 2022-08-04T09:58:52.640Z
I have set the local timezone in my centos to Asia/Calcutta and its Date() function is displaying correctly as Aug 04 2022 15:28:52 , how do I make even new Date() to output the same data in my nodejs script.
I think what you're looking for is pretty well described on this topic : How to initialize a JavaScript Date to a particular time zone
To summarize, you only have to do today.toLocaleString('IN', { timeZone: 'Asia/Kolkata' }) to get the time in India.
Please note that I'm not sure I picked the good timezone, so you might need to adjust that.
When I try to create a date object from another date format, the result date is changing it's value. How to achieve this without changing the date value ?
new Date("Mon, 31 Oct 2016 00:00:00 GMT");
the result coming as Sun Oct 30 2016 20:00:00 GMT-0400 (Eastern Daylight Time), How can I get the Monday 31 date from the above?
Adjusting the timezoneOffset from the created date object should do the trick,
but be cautious while using it , as you should be sure that the date object was created from GMT not from some local time .
And the below answer has been posted assuming the input date was in GMT
var tempDate = new Date("Mon, 31 Oct 2016 00:00:00 GMT");
var tempTime = tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000);
tempDate = new Date(tempTime);
console.log(tempDate);
It doesn't change the date, it just converts it to your local timezone. This is a bit of annoying behaviour and the only way I know to get around it is to set your system timezone to GMT. If you need to do date and time work, you might want to look at Moment.js - http://momentjs.com/
I have a string, and I have converted it into a Date variable. But the timezone is turning out to be wrong.
The string I'm trying to use is :
var v = "2013/09/05 17:53 -05:00";
var parsedvalueInField = new Date( v );
If I do an alert of parsedvalueInField,the output I get is:
Thu Sep 05 2013 18:53:00 GMT-0400 ( Eastern Daylight Time);
How do I go about rectifying this difference in Timezone?
Please Help!
2013/09/05 17:53 -05:00 is the same time as Thu Sep 05 2013 18:53:00 GMT-0400; both are Thu, 05 Sep 2013 22:53:00 GMT
In JavaScript, you have two choices (natively) about how to display a time; in the local machine's timezone (Date.prototype.toString) or in UTC (Date.prototype.toUTCString). If you want to display a time as a string with a different time zone, you will have to write a function to do it manually, calculating it from UTC.
The two main articles on MDN which will help you with how to use a Date are Date and Date.prototype.
In Javascript, how can I convert date/time in GMT to EST irrespective of user settings?
var tmpDate = New Date("enter any valid Date format here")
The javascript Date() function will automatically convert it to your local time.
Example:
var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012");
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012
Try some different values at jsfiddle: http://jsfiddle.net/R3huD/
i was surprise to find the simplest solution.
If you have date in GMT, and when you create date in browser it always create in that time zone.
Simplest way is create date object with GMT itself and then do below
starTime.setHours(starTime.getHours()+(starTime.getTimezoneOffset()/60));
That's it. Even if you have date of future after day light saving like after November then also it will also work.
See here:
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html
all you have to do is get the time in miliseconds and then add the offset in milliseconds and then shift back to a date time object