Why different dates appear the same in JavaScript? - unobtrusive-javascript

<script type="text/javascript">
alert(new Date(2010,8,31));
alert(new Date(2010,9,1));
</script>
Try the code above. The browser display the same date in both message. Why???

Date(2010,8,31) means "October 1, 2010" and
Date(2010,9,1) also means "October 1, 2010"
Because
in Date(yyyy,mm,dd), mm can be set from 0 to 11 not from 1 to 12
so that if mm is 8 means august and august have 30 days.
on this case, if you input 31 in dd, it points "August 30" + 1

Did you actually look at the alert? It displays a date in october. The months are zero-based. This means your first line is actually September 31 - which does not exist, and is wrapped to the next day, October 1. Your second line is also October 1.

Because javascript months are 0 based, like 0=Jan, 1=Feb
Since September 30 is the last day of the month, javascript corrects it to October 1.

Related

CakePHP - Date::i18nFormat() capitalization

I'm using Date::i18nFormat to display dates using the desired locale.
This is an example: $post->date->i18nFormat("dd 'of' MMMM"). This Outputs: 19 of june.
I would like to capitalize the first letter of the month, so it would output instead: 19 of June.
is it possible?
Thank you

looking for spark scala(java) code for date string with spaces in between with specific conditions

need some suggestions on below requirement.
Each response help a lot thanks in advance....
I have a date of type String with timestamp ex: Jan 8 2019 4:44 AM
My requirement is if the date is single digit I want date to be 1 space and digit
(ex: 8) and if the date is 2 digits which is dates from 10 to 31 I want date with no space(ex:10) and also same for hour in timestamp.
to summarize: if the date is 1 to 9 and hour in timestamp is 1 to 9 looking for below string
Jan 8 2019 4:44 AM
if the date is 10 to 31 and hour in timestamp is 10 to 12 looking for below string
Jan 18 2019 12:44 AM
right now I am creating a date in following way:
val sdf = new SimpleDateFormat("MMM d yyyy h:mm a")
but the above format satisfies only one condition which is dates from 1 to 9.
my application is spark with scala so looking for some spark scala code or java.
I appreciate your help...
Thanks..
java.time
Use p as a pad modifier in the format pattern string. In Java syntax (sorry):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MMM ppd ppppu pph:mm a", Locale.ENGLISH);
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 8, 4, 44)
.format(formatter));
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 18, 0, 44)
.format(formatter));
Jan 8 2019 4:44 AM
Jan 18 2019 12:44 AM
And do yourself the favour: Forget everything about the SimpleDateFormat class. It is notoriously troublesome and fortunately long outdated. Use java.time, the modern Java date and time API.
Link: Oracle tutorial: Date Time explaining how to use java.time.
To quote the DateTimeFormatter class documentation:
Pad modifier: Modifies the pattern that immediately follows to be padded with spaces. The pad width is determined by the number of pattern letters. This is the same as calling DateTimeFormatterBuilder.padNext(int).
For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.

parseexact translate everything into january in powershell

as you can see below, no matter what date i put in there , it gets translated into January.
anyone has an idea please?
thank you very much!
PS C:\Users\jy70606> [datetime]::ParseExact("20170519".trim(),'yyyymmdd',$null)
Thursday, January 19, 2017 12:05:00 AM
PS C:\Users\jy70606> [datetime]::ParseExact("20170219".trim(),'yyyymmdd',$null)
Thursday, January 19, 2017 12:02:00 AM
PS C:\Users\jy70606> [datetime]::ParseExact("20160119".trim(),'yyyymmdd',$null)
Tuesday, January 19, 2016 12:01:00 AM
PS C:\Users\jy70606>
mm is minutes. Use MM for months - E.g:
[datetime]::ParseExact("20170219".trim(),'yyyyMMdd',$null)
Check out this page on Formatting DateTime, and the Custom DateTime Format Strings page
Related: Standard DateTime Format Strings

Get week number according to your locale in Zend_Date

I am trying to get the current week number with Zend Framework.
In France, weeks are defined like this :
A week begins on Monday (whereas weeks begin on Sunday in the US).
The first week of the year is the week which contains the 4th of January.
In 2014, the first week begins January 1st. But if I use in Zend Framework 1.12 for this date like $zend_date->get(Zend_Date::WEEK) it returns 53 (and not 1). and for January 12th, it returns 1 (and not 2)
How can I correct that ? I already tried to change the locale to fr-fr but it didn't work.
Regards
Tried to replicate your problem doing
$date1 = new Zend_Date('2014-01-01');
$date2 = new Zend_Date('2014-01-12');
$date1->get(Zend_Date::WEEK) // gives 01
$date2->get(Zend_Date::WEEK) // gives 02
Also tried passing 'fr' as locale. It gives the correct answer.
I am not sure if it's a Zend issue.
Try the PHP solution:
php > echo date('W',strtotime('2014-01-01'));
01
php > echo date('W',strtotime('2014-01-12'));
02

Groovy date parse issue

date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in
println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')
yields output as
Sat Jan 22 00:00:00 GMT+05:30 2011
Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?
The problem is actualy that you are using DD - that means day in year
Correct way:
println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')
Quick tip when formatting dates try using the reverse and see what comes out:
println new Date().format("dd-MMM-yyyy")
Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.