I have a date with time and a timezone.
I need this formatted a specific way, which is why I am using moment.
Say the date string I want to put in is 2018-12-31 02:00:00.000 +00:00
I convert it into a specific format doing the following:
const properDate = moment(date, 'YYYY-MM-DD hh:mm:ss:ms')
However this is assuming the date is in UTC, and I want it to be in America/New York timezone, so the date and time I am expecting is actually:
2018-12-30 21:00:00.000
So my question is, how do I add in the timezone to moment when converting a date/time to a specific format?
I know it isn't as simple as passing a parameter. Is it another function I should call?
I have tried
const properDate = moment(date, 'YYYY-MM-DD hh:mm:ss:ms').tz('America/New_York')
but that didn't work either.
Edit: I should also mention that I am also using moment-timezone. It just seems that I can't get it to format the way I need it to as well as get it in the proper timezone.
So basically, I would think I could do something like:
const momentDate = moment(date)
const properDate = momentDate.tz(timezone).format('YYYY-MM-DD hh:mm:ss:ms')
Unfortunately as long as I try to format it, it won't work either.
Use moment.tz for parsing time string using a given timezone (e.g. 'America/New_York') while tz() is for converting between zones.
Here a live sample:
const date = '2018-12-30 21:00:00.000';
const properDate = moment.tz(date, 'YYYY-MM-DD hh:mm:ss.SSS', 'America/New_York')
console.log(properDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
Related
I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().
So I get date format like this when I query from the database:
2021-09-24T12:38:54.656Z
Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:
Sept 25 2015, 8:00 PM
I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?
Is there a workaround for this so that I don't need to change how I enter date to my db?
const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
use momentjs
You can find format method and apply as you desire
https://momentjs.com/
I think it is better to store in DB as UTC 00 value and you are doing it right now.
When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,
const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());
If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js
You'd need to create a Date object from the timestamp string like this.
let date = new Date('2021-09-24T12:38:54.656Z');
Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.
I am trying to get specific format of datetime with time zone
i am getting string of time format which is shown below
var dateTime = "2020-06-01T01:50:57.000Z CDT"
I need to convert the format in to
const offsetTime = moment(date).add("-0.00", 'hours')
const formatedDate = moment(offsetTime, 'h:mm:ss A')
.utc()
.format('h:mm A')//(1:50 AM)
Required output
(1:50 AM CDT)
Do i need to split the string and get the format or do we have any method to convert it to this format in momentjs
In simple way to say
YYYY-MM-DDTHH:mm:ss.SSS[Z] z To hh:mm A z //format
and if the string contains only 2 character like "CT" instead of CDT how to capture that.
You can zz to get timezone in output. For ex:
moment()..format('h:mm A zz')
More documentation here momentJS
Use the moment-timezone to achieve this. Use the moment constructor to specify the input format, then specifying the required timezone. Finally use moment's format to get the required format
var dateTime = "2020-06-01T01:50:57.000Z CDT";
var timezone = "America/Chicago";
console.log(
moment(dateTime, "YYYY-MM-DD hh:mm:ss zz")
.tz(timezone)
.format("h:mm A zz")
);
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
Your date string is in ISO format with the 'Z' after seconds indicating that it is in UTC time. I am assuming that the 'CDT' is placed in the string in order to indicate which time zone this should be converted to. If you have control over how this string is represented then I recommend changing it so that you indicate the desired timezone elsewhere and simply store the date in UTC format. This way you can initialize a date or moment object with the ISO string as follows:
var date = moment("2020-06-01T01:50:57.000Z")
It is inconvenient the way it is currently since you cannot initialize it this way:
var date = moment("2020-06-01T01:50:57.000Z CDT")
The only option for handling the date in its current form is to parse it. You can do that like this:
var dateTime = "2020-06-01T01:50:57.000Z CDT"
var trimmed = dateTime.trim() // remove leading and trailing whitespace
var isoString = trimmed.substr(0, trimmed.indexOf(' '))
Which will produce the following string
2020-06-01T01:50:57.000Z
You can use that string I called "isoString" to initialize a date or moment object. The next obstacle is to handle converting that UTC string to a certain timezone (in this case CDT). It is simple if you want to convert the UTC date to the current users timezone since that will happen automatically when you initialize the moment or date object with the ISO date string. Otherwise, you need some way to get the timezone from 'CDT' into the format moment wants which was shown by #vjr12 ("America/Chicago"). The only way to do this is to either store that with the date string or create a mapping. It is much easier to convert from "America/Chicago" to "CDT" than it is to convert from "CDT" to "America/Chicago". Your only option with the current form is to create your own mapping from "CDT" to "America/Chicago". You could do something like:
let tzMap = new Map()
tzMap.set('CDT','America/Chicago')
// Set the rest of your timezones
You would need to do that for all timezones and then you could use the timezone parsed from your date string like this:
var tzAbbr = trimmed.substr(trimmed.indexOf(' ') + 1)
which will grab the "CDT" or "CT" for that matter. Then you could use your mapping like this:
var timezone = tzMap.get(tzAbbr)
timezone will be "America/Chicago" in this case and then you can use #vjr12 solution from here to get the form you want.
Note
I highly recommend that (if you are able) to change the current format of the datestring that you are using. The purpose of using UTC time is to be timezone agnostic so it does not make sense to store the timezone with the UTC string. If you want to preserve the timezone then you would be better off using a format which already embeds the timezone.
I fetch invalid date strings from the REST API but i may not fix the REST API. How can i format an invalid date strings like that "20180517T010237" ?
I tried to use moment for that, but i couldnt succeed.
let date = moment("20180517T010237", "YYYY-MM-DD T HH.mm.ss").toDate();
is there any easy way to do that?
The second string you pass moment is the format of the string you're parsing. Your format string has - and spaces that aren't in your input. Remove them:
let date = moment("20180517T010237", "YYYYMMDDTHHmmss").toDate();
Note that it will be parsed in local time. If you want UTC instead, use moment.utc:
let date = moment.utc("20180517T010237", "YYYYMMDDTHHmmss").toDate();
Example:
let date = moment("20180517T010237", "YYYYMMDDTHHmmss").toDate();
console.log(date);
date = moment.utc("20180517T010237", "YYYYMMDDTHHmmss").toDate();
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
I am trying to get the date time in moment js in this format :
2016-12-19T09:43:45.672Z
The problem is I am able to get the time format as
2016-12-19T15:04:09+05:30
using
moment().format();
but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.
From the documentation on the format method:
To escape characters in format strings, you can wrap the characters in square brackets.
Since "Z" is a format token for the timezone, you need to escape it. So the format string you are after is:
moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
As #VincenzoC said in a comment, the toISOString() method would also give you the format you are after.
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment().toISOString();
Try this
const start_date = '2018-09-30';
const t = moment(start_date).utc().format();
console.log(t);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
var dateTime = new Date("2015-06-17 14:24:36");
dateTime = moment(dateTime).format("YYYY-MM-DD HH:mm:ss");
Try this. You should have the date in the correct format.
I am working with momentjs and converting dates to different time zones using convertedDate = moment().utcOffset(timezone).format(). This works well but it is a string and I need to transform it to date object.
I've tried new Date(convertedDate) and moment().utcOffset(timezone).toDate() but that returns my current timezone as a date object. How can I keep the converted timezone?
So I wasn't very far off. The format needs to exclude timezone for it to work. This code finally worked how I needed it to.
convertedDate = new Date(moment().utcOffset('-4').format('YYYY-MM-DD HH:mm'));
A cleaner approach to get a native Date object with time according to the timezone, using moment would be following:
convertedDate = moment.utc(moment.tz(timezone).format('YYYY-MM-DDTHH:mm:ss')).toDate()
PS: assuming two things
you have imported both 'moment' and 'moment-timezone'.
value of timezone is given like 'Asia/Kolkata' instead of an offset value
This should work:
I have the same issue. Just get the Date as a string using the same approach that you are using. Let's say your date is, for example: '2018-08-05T10:00:00'.
Now you need the Date object with correct time. To convert String into object without messing around with timezones, Use getTimezoneOffset:
var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() - userTimezoneOffset);
getTimezoneOffset() will return either negative or positive value. This must be subtracted to work in every location in the world.