Converting a date string into UTC+0530 format using javascript - javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?

Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.

var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Related

Transform date to wire format (YYYY-MM-DD) in javascript

Having this input: Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time), is there a way to format it as YYYY-MM-DD, to it will become 2021-02-03?
Try this:
const date = moment(new Date('Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)')).format('YYYY-MM-DD');
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Parsing a timestamp to a date to reformat it may well produce incorrect results if the timestamp includes an offset or timezone. Given "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)", for a user with an offset of +1 or less the date will be 2 Feb, not 3 Feb.
The most reliable method is to reformat the string, e.g.
let timestamp = "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)";
let months = [,'Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
let pad = n=>('0'+n).slice(-2);
let p = timestamp.split(' ');
console.log(`${p[3]}-${pad(months.indexOf(p[1]))}-${p[2]}`);

javascript Date.toISOString() return difference date value

I'm confusing about the javascript Date.toISOString() function which shown as below example, how come date value of x in ISO format become January?
const date = new Date();
const x = (new Date(date.getFullYear(), date.getMonth() , 1));
console.log(date); \\Tue Feb 04 2020 11:11:12 GMT+0800 (Malaysia Time)
console.log(x); \\Sat Feb 01 2020 00:00:00 GMT+0800 (Malaysia Time)
console.log(date.toISOString()); \\2020-02-04T03:11:12.330Z
console.log(x.toISOString()); \\2020-01-31T16:00:00.000Z
This is due to time zone conversion from GMT+08 to UTC. The toISOString function converts the date to UTC (as a note you can determine that the date is in the UTC time zone by "Z" at the end of the string).
When converting Feb 01 2020 00:00:00 GMT+0800 to an ISO string, the date is reduced by 8 hours and hence becomes Jan 31 2020 16:00:00.

Create date from number of days

I am trying to convert the number of days since Jan 01 1970 to JavaScript Date.
Here is the code snippet.
new Date(864e5 * parseInt(data[i].d));
//here data[i].d contains number of days.
I checked all the data by this.
console.log(typeof(data[i].d), data[i].d);
//prints
number 17674
but sometimes it unable to convert it into date.
Invalid Date {}
while for
number 17858
//outputs.
Fri Aug 17 2018 05:00:00 GMT+0500 (Pakistan Standard Time)
Thanks for your time.
You just have to add the number of days times the milliseconds in a day, like so:
var originalDay = new Date(864e5)
console.log(originalDay) //Thu Jan 01 1970 19:00:00 GMT-0500 (Eastern Standard Time)
var numOfDays = 7
var daysSince = new Date(864e5 + parseInt(numOfDays * 864e5))
console.log(daysSince) //Thu Jan 08 1970 19:00:00 GMT-0500 (Eastern Standard Time) --7 days later
To make this work for you, you would just have to replace that numOfDays with the values in your array.

How to set date always to eastern time regardless of user's time zone

I have a date given to me by a server in unix time: 1458619200000
NOTE: the other questions you have marked as "duplicate" don't show how to get there from UNIX TIME. I am looking for a specific example in javascript.
However, I find that depending on my timezone I'll have two different results:
d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)
// Now I set my computer to Eastern Time and I get a different result.
d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)
So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?
You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,
var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)
dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)
var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)
Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!
Moment.js (http://momentjs.com/timezone) is your friend.
You want to do something like this:
var d = new Date(1458619200000);
var myTimezone = "America/Toronto";
var myDatetimeFormat= "YYYY-MM-DD hh:mm:ss a z";
var myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);
console.log(myDatetimeString); // gives me "2016-03-22 12:00:00 am EDT"
For daylight saving, Eastern time become 4 hours behind UTC. That's why its offset is -4x60 = -240 minutes. So when daylight is not active the offset will be -300. The offset variable's value is the key point to be noted here. Kindly see this code in action in attached image.
var offset = new Date().getTimezoneOffset();// getting offset to make time in gmt+0 zone (UTC) (for gmt+5 offset comes as -300 minutes)
var date = new Date();
date.setMinutes ( date.getMinutes() + offset);// date now in UTC time
var easternTimeOffset = -240; //for dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300
date.setMinutes ( date.getMinutes() + easternTimeOffset);

Javascript UTC timestamp to Local Timezone

I'm trying to convert a timestamp being returned from a JSON resource in javascript that is displaying in UTC to the users local timezone. Below i'm trying to adjust with the user offset.
Example UTC output for date:
Tue Mar 27 2012 02:29:15 GMT-0400 (EDT)
Code
var date = new Date(data.date_created); //Data.date_created coming from json payload
var offset = date.getTimezoneOffset() //Get offset
var new_date = new Date(date offset); //Add offset to userdate
I'm struggling with the appropriate method to achieve this. Can anyone point me in the right direction?
I might be missing something but
var date = new Date( data.date_created );
does what I think you want.
>>> d=new Date('Tue Mar 27 2012 02:29:15 GMT-0800')
Date {Tue Mar 27 2012 06:29:15 GMT-0400 (EDT)}
>>> d.toLocaleString()
"Tue Mar 27 06:29:15 2012"
>>> d=new Date('Tue Mar 27 2012 02:29:15 GMT+0300')
Date {Mon Mar 26 2012 19:29:15 GMT-0400 (EDT)}
>>> d.toLocaleString()
"Mon Mar 26 19:29:15 2012"
Note how changing the GMT offset from -8 to +3 changes the resulting time by 11 hours.

Categories

Resources