Formatting a date as a UTC date with Datejs [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I need to display a date formatted as a UTC date, and I should do this with Datejs. Is this possible?
I need to do something like
var d = new Date();
var dateString = d.toString("d \n MMM/yyyy \n H:mm:ss");
...but with date and time formatted as UTC.
I looked at the docs but the closest thing I found is
d.toISOString();
which gives me a date as a UTC string but not in the format I want.
edit: changed toUTCSTring to toISOString

Consider using Moment.js, a more well-maintained library for working with dates and times.
moment.utc(d).format("D [\n] MMM/YYYY [\n] H:mm:ss");
If this is not an option, you can shift the date by the timezone offset (which gives you the date/time portion of the UTC date but in the local timezone), then format it:
var d2 = new Date(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
console.log(d2.toString("d \n MMM/yyyy \n H:mm:ss"));
Edit: Removed dependency on built-in date parser.

Related

how to convert date object to string using javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 11 months ago.
I have a date object as follows
let dateObj = Sat May 01 2021 20:21:00 GMT-0700 (Pacific Daylight Time)
I want to convert above value in this string format.
let stringVal = 2021-05-01T20:21:00.000+0000
can someone let me know how to achieve this. i Have tried toUTCString(), to DateString(), toISOString() and all other methods but i was not able to achieve the result in the format above.
Any guidance is appreciated. is it even possible ?
Not sure if you wanted it converted to UTC, but based on your example it looks like not.
Maybe something like this? Just replace "new Date()" with your date object.
const date = new Date(new Date() - 1000 * 60 * 60 * 7); // subtract 7 hours since .toISOString() is going to add 7 hours
console.log(date.toISOString().replace('Z','+0000'));
* I should add this assumes the client's timezone is Pacific Daylight Time, or a least something with the same offset

How to extract date and time from ISO into local date and time [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
I have a date and time in ISO
"time": "2021-10-28T17:30:00.000Z"
Below is what I am doing right now to extract the date and time:
var dateExtract = time.substring(0, 10);
var timeExtract = time.match(/\d\d:\d\d/);
But it is giving me the exact date and time written in ISO.
What I want is to extract the date and time in local date and time. I don't know how to do this.
Use the Date instance.
let obj = {"time": "2021-10-28T17:30:00.000Z"}
const dateTime = new Date(obj.time);
const date = dateTime.toLocaleDateString().replace(/\//g, '-');
// '28-10-2021'
const time = dateTime.toLocaleTimeString()
// '18:30:00' since I am in (+1)
Also find a pool of answers in this similar Question
Just create a date object and use toLocaleString or toLocaleDateString or
toLocaleTimeString
const date = new Date("2021-10-28T17:30:00.000Z")
console.log(date.toLocaleString())
console.log(date.toLocaleDateString().replaceAll("/","-"))
console.log(date.toLocaleTimeString())
You are getting dates in a wrong method.
when you are reading the date it should be converted to your locale, so that you will get exact date from UTC date
You can use JS Date() function or moment library
let d = new Date("2021-10-28T17:30:00.000Z")
d.toDateString()
d.toLocaleDateString()
check Date functions to get date, month, year, time , seconds or timestamp
refer How to format a JavaScript date
https://www.w3schools.com/js/js_dates.asp
https://www.w3schools.com/js/js_date_formats.asp
Or you can use the external moment library
https://momentjs.com/

JavaScript input date convertion

Is there any way to convert any date string (not necessarily current date) (could be any format) to specific date format in Javascript. Like converting "MM-DD-YYYY" or "ddMMYYYY" to "DD-MMM-YYYY"?
I know that from current date as var date = new Date(), we can get time and hours but what to do in case of existing date string like "31/01/1999" to "31-JAN-1999".
Given the input date string can be of any format.
This is a common problem.
You should be able to do it with moment.js.
Ex.
moment("31/01/1999").formatWithJDF("dd - MM - yyyy");
Have a look here, https://momentjs.com/docs/ for more details.
Using DateFormatter.js
var date = new Date('2020-03-25 10:30:25');
var formatter = new DateFormatter();
displayFormat = 'D M d Y h:i:s';
var dateString = formatter.formatDate(date, displayFormat); // Wed Mar 25 2020 10:30:25
This can be done by first checking the input date format with the arrays or Map of regex provided, then need to convert that format to JS accepted format ISO format.
Once converted to ISO format this date now can be converted to any form with logic written for it in separate functions.

Javascript dates are a day off? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Changing the format of a date string
(2 answers)
Closed 5 years ago.
I am trying to use a simple date function in my application to pass a date in the format of yyyy-mm-dd such as 2017-07-30 and have it returned in the format of 07/30/2017.
However, when I try this, I supply my date correctly but it outputs one day shorter than what I am looking for.
function format(inputDate) {
var date = new Date(inputDate);
if (!isNaN(date.getTime())) {
var day = date.getDate().toString();
var month = (date.getMonth() + 1).toString();
// Months use 0 index.
return (month[1] ? month : '0' + month[0]) + '/' +
(day[1] ? day : '0' + day[0]) + '/' +
date.getFullYear();
}
}
console.log(format('2017-07-30'));
Here is a fiddle: http://jsfiddle.net/49pptrj4/
Any thoughts as to why this is returning incorrectly?
Result on my end:
From here
Given a date string of "March 7, 2014", [Date.]parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC.
Your date string is assumed to be 0:00, or midnight, on the date specified in UTC, the time zone of Greenwich, England. Your browser however takes this time and converts it to your local timezone, which is a few hours behind UTC if you're in the Americas, making the result a day behind.
The following code should work for creating a Date in the local timezone with the correct date.
utcDate = new Date("2017-07-30"); //Date object a day behind
new Date(utcDate.getTime() + utcDate.getTimezoneOffset() * 60000) //local Date
Here the local Date is created by adding time based on the time zone difference. getTimezoneOffset() returns in minutes, so * 60000 is needed to convert to milliseconds.
This might not work in areas ahead of UTC; it might advance an extra day.
Edit: Just checked and getTimezoneOffset() is negative in areas ahead of UTC so it will subtract time correctly.

How to convert Date to Other Time Zone in Javascript [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 8 years ago.
I am trying to convert Local Date/Time to other Time Zone with JavaScript.
Dates are stored in DB as UTC.
For example,
value = "2014-08-15T11:09:10Z"
var dt = new Date(value)
the output will be in my Local Timezone
Fri Aug 15 2014 18:09:10 GMT+0700 (ICT)
But how can I convert this to other Time Zone (i.e - Moscow) by using JavaScript.
You may try like this:
function myTimeZOne(value, zone) {
var f = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(value, f).tz(zone).format(f);
}
Also check moment.js
You can use JS library such as timezone-js. You could write code like this :
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
var dt = new timezoneJS.Date(format , 'Europe/London');
dt.setTimezone("Asia/Jakarta");
you can also check out other JS library like:
MomentJS
DateJS

Categories