How to format date without time using toLocaleTimeString - javascript

I have a Date, which I want to convert to a string, but without the time, just the date part.
My current code:
var date = new Date(2014, 9, 08); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleTimeString("en-US", options));
// output: Wednesday, October 8, 2014 12:00:00 AM
// what I'm looking for: Wednesday, October 8, 2014
How can I modify the options to not display time?

Juste use toLocaleDateString instead of toLocaleTimeString and you should get the result you are expecting :
var date = new Date('2014', '9', '08'); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleDateString("en-US", options));
returns : "Wednesday, October 8, 2014"
I will also second the above poster and recommend using moment.js; it is lightweight and very flexible.

If you are satisfied with allowing the browser to determine the display format, you can use the toDateString or toLocaleDateString functions of the Date object. You will get different results on different browsers.
Otherwise, consider using a library such as moment.js, so you can control the format to your liking. For example:
moment([2014,9,8]).format('dddd, MMMM Do, YYYY') // "Wednesday, October 8, 2014"

Related

Change timezone format

after chaning my date timezone using toLocalString()
new Date(dateParam).toLocaleString('en-US', { timeZone: "Australia/Sydney" })
i've got this string 5/25/2022, 8:44:46 PM, how can i transform from this format to default new Date() fromat Tue Jul 05 2022 23:22:37 GMT+0300, to be able to use Date methods(example: toDateString(), getFullYear())?
add to your solution two parameters dataStyle and timeStyle like in the example below
console.log(new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long' }).format(date));
// Expected output "Sunday, 20 December 2020 at 14:23:16 GMT+11"

How can I convert my date "Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) " to JAN 01,2020 using JavaScript?

Currently I am getting my date from data base in the format of Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) but I want the date in JAN O1,2020 format and I don't want to use mon=ment.js for this. any other ways to achieve this?
Here is a solution without moment.js
var n = new Date("Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)");
var options = {
month: "short",
day: "numeric",
year: "numeric",
timeZone: 'IST'
}
document.write(n.toLocaleString("en-EN", options));
You can simply use moment.js
import * as moment from 'moment';
moment('your_date_variable').format('DD/MM/YYYY');

Why startOfMonth results in different timezone that endOfMonth

I am trying to get proper start and end of month values using date-fns. My browser timezone is UTC+2 (as new Date().getTimezoneOffset() results in -120).
Example code:
console.log('start of month: ', dateFns.startOfMonth(new Date()));
console.log('end of month: ', dateFns.endOfMonth(new Date()));
result:
start of month: Thu Oct 01 2020 00:00:00 GMT+0200 (czas środkowoeuropejski letni)
end of month: Sat Oct 31 2020 23:59:59 GMT+0100 (czas środkowoeuropejski standardowy)
Why startOfMonth results in timezone GMT+0200 while endOfMonth results in GMT+1? Is it possible to get proper values of timezones (GMT+0200) for both cases?

Set last date before save angular js

I am trying to change date value before save. according to my requirement, i need to set last date of the month before save. this is my save function
$scope.saveCashBenefits = function (a) {
var olddate = $scope.newcashbenifit.toDate;
//for an example olddate = Wed Apr 01 2020 00:00:00 GMT+0530
}
In the above sample, the olddate is 'Wed Apr 01 2020 00:00:00 GMT+0530'
I need to modify this as 'Apr 30 2020'
if olddate is Month 'May', I need to set my month as 'May 31 2020'
basically i need to set last day of the month.
How I do it?

momentJS convert possibility day into Date() js format

I am trying to convert a date in format momentjs into a date from javascript native new Date().
The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.
myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...
It is possible to convert it like that way?
Don't need moment:
let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"
you want something like this:
moment(myDay, "YYYY-MM-DD").toString();
moment().toString() Returns an english string in a similar format to JS Date's .toString().
moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"

Categories

Resources