Want to remove TimeZone details from date+time.(EmberJS)
Input: "2019-03-11T09:00:00.000+09:00" (GMT+9) .
Like to convert in this format "2019-03-11T09:00:00.000+00:00" (GMT+0)
Usecase:
API Returning : timestamp: "2019-03-11T09:00:00.000+09:00"
UI TimeZone is : UTC+9
Currently UI Display: 3/11/2019 18:00
UI should show 3/11/2019 9:00
Try using ember-moment addon
{{moment-format '12-1995-25' 'MM/DD/YYYY' 'MM-YYYY-DD'}}
Replace 12-1995-25 with your API returning date
If you just want to drop timezone from date that you receive as a string, you just need to use regular expression to remove '+09:00' part:
new Date('2019-03-11T09:00:00.000+09:00'.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})[+\-]\d{2}:\d{2}/, '$1'))
Or, to replace timezone with UTC:
new Date('2019-03-11T09:00:00.000+09:00'.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})[+\-]\d{2}:\d{2}/, '$1+00:00'))
If you want to convert between timezones you can use moment-timezone or luxon
Related
I want to manipulate date which come from the api.
When I use: console.log(dataAPI.dateStation)
I see 2023-01-24T06:00:00.000Z
Is there way to change the date time in this format 2023-01-24 06:00:00
Just I want to remove T character between date and time and remove .000Z at the end.
The simplest way to do it is probably:
new Date(dataAPI.dateStation).toLocaleString()
If what you want is to display it somewhere, it'll automatically adapt the ISO date you have into a localized and readable date (based on timezone and language).
To know more about it and the options, here is the doc: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
If you want to print the date in ISO 8601 format, you can use the 'sv' (Sweden) locale and Date.toLocaleString().
You can also specify whichever IANA timezone you wish to use, I'm using UTC in this case.
const d = '2023-01-24T06:00:00.000Z'
let timestamp = new Date(d).toLocaleString('sv', { timeZone: 'UTC' });
console.log('Timestamp:', timestamp);
Use the javascript date class with toLocaleString
new Date(dataAPI.dateStation).toLocaleString('en-US');
Without installing some third-party library, your best bet is probably to use the string you have (which is the format returned by toISOString() ),and modify it as desired. If it's already a string in the format you gave, you can just call replace on it:
dataAPI.dateStation.replace('T',' ').replace('.00Z','')
If it's a Date object, first call toISOString() to get a string:
dataAPI.dateStation.toISOString().replace('T',' ').replace('.00Z','')
If it's a string in a possibly-different format, call new Date() to get a Date object, then call toISOString() on that, and finally call replace on the result:
new Date(dataAPI.dateStation).toISOString().replace('T',' ').replace('.00Z','')
You can use regular expressions to remove the parts you don't want:
let s = "2023-01-24T06:00:00.000Z"
s = s.replace(/T/, ' ')
s = s.replace(/\.\d{3}Z$/, '')
console.log(s)
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 have date in 2019-12-07T14:55:00.000Z which I want to convert to target format without effecting original value by browser's timezone using momentjs. I tried moment('2019-12-07T14:55:00.000Z').format('MM/DD/YYYY HH:mm'), but it is converting it to 12/07/2019 6:55 (because browser timezone is PST) whereas I expect it to be 12/07/2019 14:55
we can use utc function to prevent moment to use browser's timezone , just like below
moment.utc('2019-12-07T14:55:00.000Z').format('MM/DD/YYYY HH:mm')
I want to show in a view of an angular project a human friendly date format, and I saw that angular has a filter to do so, but the input date needs to have a certain format.
The desired output format to be seen in the view is the following: "dd/MMMM/yyyy hh:mm:ss"
Currently, I have on a Database the following timestamp format:
"2016-08-15 12:34:34"
How can I format this type of timestamp so that angular can intepret it and format it as desired?
Thanks a lot!
You can parse the date to a javascript date and then use the date filter in Angular to show it in the format you want (if you can't use the date filter on the date from your database directly).
You could do this as a filter to make it easier:
myApp.filter('formatted', function() {
return function(value) {
return new Date(value);
}
});
In your html you could then add this filter before the date filter:
Filter: {{ vm.dateToFormat | formatted | date: 'dd/MMMM/yyyy hh:mm:ss' }}
If Angular is having trouble parsing the string from the database on its own you could just use JavaScript Date.parse("2016-08-15 12:34:34") to return you a date that Angular will interpret properly.
You could just use the inline filter:
Example:
<span>{{vm.Date | date: 'dd/MMMM/yyyy hh:mm:ss'}}</span>
EDIT: To be clear, you post doesn't clarify if that timestamp is a string or not, so you may have to parse it accordingly. I see the other posters just assumed it was a string.
I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!